无法使用角度ui路由器访问控制器

时间:2015-02-10 22:12:32

标签: angularjs angular-ui-router

我在我的应用程序中使用Angular ui路由器进行路由,但我想出了一个特殊的问题,因为我无法访问ProfileCtrl。我的控制台完全没有出现任何错误,这使得这更加奇怪。我可以访问除ProfileCtrl之外的所有其他控制器。我不知道如何调试这个问题,但似乎我的路线无法“读取”ProfileCtrl中的任何内容,就好像我删除了所有仍未显示错误的内容。到目前为止这是我的代码。

angular.module('Chat', ['ngResource', 'ngMessages', 'ui.router', 'ngAnimate', 'ngSanitize', 'satellizer', 'mgcrea.ngStrap', 'angularFileUpload'])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {     
        $stateProvider
            .state('home', {
                url: '/',
                templateUrl: 'views/home.html',
                controller: 'MainCtrl'
            })
            .state('login', {
                url: '/login',
                templateUrl: 'views/login.html',
                controller: 'LoginCtrl'
            })
            .state('profile', {
                url: '/profile',
                templateUrl: 'views/profile.html',
                controller: 'ProfileCtrl',
                resolve: {
                    authenticated: function($q, $location, $auth) {
                        var deferred = $q.defer();

                        if (!$auth.isAuthenticated()) {
                            $location.path('/login');
                        } else {
                            deferred.resolve();
                        }

                        return deferred.promise;
                    }
                }
            });

        $urlRouterProvider.otherwise('/');
 }]);

//控制器

angular.module('Chat')
.controller('ProfileCtrl', ['$scope', function($scope) {

    console.log("test");


}]);

1 个答案:

答案 0 :(得分:0)

您的.state正在使用resolve选项。根据angular-ui Wiki

  

resolve是一个可选的依赖关系图,应该注入控制器。

     

如果这些依赖项中的任何一个是promise,它们将在实例化控制器并触发$ stateChangeSuccess事件之前被解析并转换为值。

但是,您的控制器不接受值authenticated作为依赖项。尝试更改签名

.controller('ProfileCtrl', ['$scope', 'authenticated' function($scope, authenticated) {

并验证您的依赖项实际上是返回一个值;现在,看起来它没有返回任何东西。