从角度js中的对象访问对象数组

时间:2015-08-05 13:26:00

标签: javascript angularjs

我有这些功能:

getPermissions: function(){
        return currentUser.permissions;
       }, //It returns permissions which is an array of strings as well.I use it in resolve first, before loading the page and it works

hasRole: function(){
        console.log(currentUser);
      return currentUser.role; //role is an array of objects but gives undefined
       }

currentUser是一个看起来像这样的对象:

currentUser

currentUser.role可能未定义,因为promise还没有解决。我必须在登录后根据currentUser.role中包含的值重定向用户。我如何能 ?先解决然后重定向?我认为它确实应该解决问题。

编辑:我在名为auth.js的文件中有上述功能。 auth.js也有登录功能,如下所示:

 login: function(user, callback) {
        var cb = callback || angular.noop;
        var deferred = $q.defer();

        $http.post('/auth/local', {
          email: user.email,
          password: user.password
        }).
        success(function(data) {
          $cookieStore.put('token', data.token);
          currentUser = User.get();
          deferred.resolve(data);
          return cb();
        }).
        error(function(err) {
          this.logout();
          deferred.reject(err);
          return cb(err);
        }.bind(this));

        return deferred.promise;
      }

我正在auth.js中的login.controller.js调用登录功能。

  $scope.submitted = true;

      if(form.$valid) {
        Auth.login({
          email: $scope.user.email,
          password: $scope.user.password
        })
        .then( function() {
          // Logged in, redirect to home
        var role = Auth.hasRole();
        console.log("role");
        console.log(role);  //gives undefined
       if(role.priority >= 1){
          $location.path('/admincontrol');

        }else{
          $location.path('/');

        }

        })
        .catch( function(err) {
          $scope.errors.other = err.message;
        });
      }
    };

0 个答案:

没有答案