Using Resolve in AngularJS

1

I'm using resolves to verify that the user is logged in as follows:

routeProvider.
//...
.when('/dashboard', {
            title: 'Dashboard',
            templateUrl: 'view/dashboard.html',
            controller: 'authCtrl',
            resolve: {
                auth: function ($q, Session) {
                    var userInfo = Session.getUserInfo();
                    if (userInfo) {
                        return $q.when(userInfo);
                    } else {
                        return $q.reject({ authenticated: false });
                    }
                }
            }
        })

My question is, what does the $ q.when (userInfo); in the above code?

So I understand, you should pass the variable "userInfo" as a parameter in the "routeChangeSucess" event, right?

app.run(["$rootScope", "$location", 
  function ($rootScope, $location) {

    $rootScope.$on("$routeChangeSuccess", function (userInfo) {
        console.log(userInfo);
    });
    //...

Note that I have in the code above a console.log, but when checking the Firefox console, it does not return the user's data, but instead the following:

  

Object {name="$ routeChangeSuccess", targetScope = h, defaultPrevented = false, more ...}

What's wrong?

    
asked by anonymous 30.10.2014 / 21:30

1 answer

0

In fact what is wrong are the parameters of $routeChangeSuccess , they should be:

$rootScope.$on("$routeChangeSuccess", function(event, current, previous) {

So the log shows this object:

  

Object {name="$ routeChangeSuccess", targetScope = h, defaultPrevented = false, more ...}

On $q.when , it does nothing more than get a promise or any object and return a promise . If this object is not a promise , when makes a wrap it in a promise and calls resolve .

    
31.10.2014 / 14:42