Angularjs通过解析函数传递变量

时间:2015-05-06 17:15:07

标签: javascript angularjs angular-ui-router

现在,当我的状态转到:cloud_id时,我设置了一个Auth Factory。我试图将authCheck()传递给resolve: { authenticated: authenticated } 函数,但我不知道如何。

有人能指出我正确的方向改变这个:

resolve: { authenticated: authenticated(cloud_id) }

对于这样的事情:

.factory('Auth', function($http, $state, $q) {

    var factory = { authCheck: authCheck };
    return factory;

    function authCheck(cloud_id) {
        return $http.post('/auth/check', {id:cloud_id});
    }

})
.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $locationProvider.html5Mode(true);
    $urlRouterProvider.otherwise('/cloud');

    var authenticated = ['$q', 'Auth', '$rootScope', function ($q, Auth, $rootScope) { 
        var deferred = $q.defer();
        Auth.authCheck(cloud_id).then(function(){ deferred.resolve(); }, function(){ deferred.reject(); });
        return deferred.promise;
    }];

    $stateProvider

        .state('cloud', {
            url: '/cloud/:cloud_id',
            templateUrl: 'pages/templates/cloud.html',
            controller: 'cloud',
            resolve: { authenticated: authenticated }
        })

})

这是我的代码:

mget

1 个答案:

答案 0 :(得分:3)

您可以像这样注入和使用$stateParams

var authenticated = ['$q', 'Auth', '$rootScope', '$stateParams', function ($q, Auth, $rootScope, $stateParams) { 
    var deferred = $q.defer();
    Auth.authCheck($stateParams.cloud_id).then(function(){ deferred.resolve(); }, function(){ deferred.reject(); });
    return deferred.promise;
}];

请参阅this plnkr