【发布时间】:2017-01-31 19:03:51
【问题描述】:
我正在使用ui.router 来检测 Angular App 中的状态变化。但是控制器没有在加载时被调用。
下面是我的app.js 代码。
var admin = angular.module('admin',['admin.core']);
angular.module('admin.core', ['ui.router', 'satellizer', 'ngMaterial']);
admin.config(function($mdThemingProvider, $stateProvider,
$urlRouterProvider, $authProvider, $locationProvider){
$mdThemingProvider.theme('default').primaryPalette('light-blue', {
'default': '700',
'hue-1' : 'A200',
}).accentPalette('blue');
// Satellizer configuration that specifies which API route the JWT should be retrieved from
$authProvider.loginUrl = '/api/authenticate';
// Redirect to the auth state if any other states are requested other than users
$urlRouterProvider.otherwise('/admin/login');
console.log($stateProvider)
$stateProvider
.state('login', {
url: '/admin/login',
name: 'login',
controller: 'AuthController as auth',
})
.state('users', {
url: '/admin/users',
controller: 'UserController as user'
});
$locationProvider.html5Mode(true);
});
admin.controller('AuthController', AuthController);
function AuthController($auth, $state, $scope) {
console.log("Called");
var vm = this;
vm.login = function() {
var credentials = {
email: vm.email,
password: vm.password
}
console.log(credentials);
// Use Satellizer's $auth service to login
$auth.login(credentials).then(function(data) {
// If login is successful, redirect to the users state
$state.go('users', {});
});
}
}
当我的状态为login时,不会调用AuthController。
【问题讨论】:
-
为什么是 url:'users',而不是 '/users'?并添加名称:'users'
-
@PiotrPęczek .. 更正
-
您的根应用 html 中是否有
ui-view? -
是的。在我的主 html 中,应用程序已初始化。我有 ui-view 那里
-
检查控制台是否有任何错误
标签: angularjs angular-ui-router-extras