无法使用 angular ui 路由器访问控制器
Cannot access controller using angular ui router
我在我的应用程序中使用 Angular ui 路由器进行路由,但我遇到了一个特殊问题,因为我无法访问 ProfileCtrl。我在我的控制台中根本没有收到任何错误,这让这更奇怪了。我可以访问除 ProfileCtrl 之外的所有其他控制器。我不知道如何调试这个问题,但似乎我的路线无法 "read" 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");
}]);
您的 .state
正在使用 resolve
选项。根据 angular-ui Wiki:
resolve is an optional map of dependencies which should be injected into the controller.
If any of these dependencies are promises, they will be resolved and converted to a value before the controller is instantiated and the $stateChangeSuccess event is fired.
但是,您的控制器不接受值 authenticated
作为依赖项。尝试更改您的签名
.controller('ProfileCtrl', ['$scope', 'authenticated' function($scope, authenticated) {
并验证您的依赖项是否确实返回了一个值;现在,它似乎没有返回任何东西。
我在我的应用程序中使用 Angular ui 路由器进行路由,但我遇到了一个特殊问题,因为我无法访问 ProfileCtrl。我在我的控制台中根本没有收到任何错误,这让这更奇怪了。我可以访问除 ProfileCtrl 之外的所有其他控制器。我不知道如何调试这个问题,但似乎我的路线无法 "read" 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");
}]);
您的 .state
正在使用 resolve
选项。根据 angular-ui Wiki:
resolve is an optional map of dependencies which should be injected into the controller.
If any of these dependencies are promises, they will be resolved and converted to a value before the controller is instantiated and the $stateChangeSuccess event is fired.
但是,您的控制器不接受值 authenticated
作为依赖项。尝试更改您的签名
.controller('ProfileCtrl', ['$scope', 'authenticated' function($scope, authenticated) {
并验证您的依赖项是否确实返回了一个值;现在,它似乎没有返回任何东西。