6

Right now I have a Auth Factory set up to run when my State goes to cloud. I am trying to pass the :cloud_id to that authCheck() function but I don't know how.

Could anyone point me in the right direction to change this:

resolve: { authenticated: authenticated }

To something like this:

resolve: { authenticated: authenticated(cloud_id) }

Here is my code:

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

})
2
  • 1
    You can inject $route in your resolve function and get the params out of there: stackoverflow.com/a/13433335/373655 Commented May 6, 2015 at 17:17
  • Thanks @rob, that last comment about ui-router and $stateParams helped a lot Commented May 6, 2015 at 17:43

1 Answer 1

3

You can inject and use the $stateParams like this:

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;
}];

See this plnkr.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.