Angularjs通过解析函数传递变量

bry*_*yan 5 javascript angularjs angular-ui-router

现在我有一个Auth Factory设置在我的州去的时候运行cloud.我试图传递:cloud_id给那个authCheck()功能,但我不知道如何.

任何人都可以指出我正确的方向来改变这个:

resolve: { authenticated: authenticated }
Run Code Online (Sandbox Code Playgroud)

对于这样的事情:

resolve: { authenticated: authenticated(cloud_id) }
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

.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 }
        })

})
Run Code Online (Sandbox Code Playgroud)

Ret*_*old 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;
}];
Run Code Online (Sandbox Code Playgroud)

请参阅此 plnkr