我正在使用ui.router
來檢測Angular App中的狀態更改。但是控制器不會在加載時被調用。ui.router狀態不按預期方式工作
以下是我的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', {});
});
}
}
AuthController
當我的狀態是login
不叫。
爲什麼url:'users',而不是'/ users'?並添加名稱:'用戶' –
@PiotrPęczek..更正 – Gags
你有'ui-view'在你的根app html的某個地方嗎? – devqon