gpt4 book ai didi

javascript - Angular UI-Router 缩小错误 - 如何将解析语法更改为基于字符串注入(inject)?

转载 作者:可可西里 更新时间:2023-11-01 01:48:45 25 4
gpt4 key购买 nike

我正在使用带有解析函数的 Angular UI-Router,但是当我缩小解析函数时,我的整个应用程序都崩溃了,因为解析函数语法不适合缩小。它需要是基于字符串注入(inject)的概述 here .我只是不确定如何写它。有什么建议吗?

// Resolves
var checkAuthentication = function($q, $location, $rootScope, Users) {
if ($rootScope.user) return true;
if (!$rootScope.user) {
var deferred = $q.defer();
Users.get(null, function(user) {
if (!user) {
window.location = '/';
return false;
}
console.log('User fetched: ', user);
$rootScope.user = user;
deferred.resolve();
}, function() {
window.location = '/';
return false;
});
return deferred.promise;
}
};

// Routes
angular.module('Dashboard').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For any unmatched url, redirect to '/'
$urlRouterProvider.otherwise('/dashboard');
// Now set up the states
$stateProvider
.state('dashboard', {
url: '/dashboard',
templateUrl: 'views/content/dashboard.html',
resolve: {
checkAuthentication: checkAuthentication
}
})

最佳答案

您通常执行此操作的方式是将数组传递给解析:

resolve: {
welcome: ['$q', function ($q) {
var def = $q.defer();
setTimeout(function () {
def.resolve("Hello World!");
},500);
return def.promise;
}]
}

考虑到这一点,您可以像这样定义您的 var:

var welcome = ['$q', function ($q) {
var def = $q.defer();
setTimeout(function () {
def.resolve("Hello World!");
},500);
return def.promise;
}]

但这当然不是真正可重用的,所以在这一点上我建议将其移至服务。

关于javascript - Angular UI-Router 缩小错误 - 如何将解析语法更改为基于字符串注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24636171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com