I am using ui.router
to detect state change in Angular App. But the controller is not being called on load.
Below is my app.js
code.
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
is not called when my state is login
.
When using the $locationProvider.html5Mode(true);
you have to provide a base href location. It will parse your state resolve url as http://localhost:8001/admin/#/login
and should fix your problem.
Try adding <base href="/" />
to your <head />
section of your main index file to specify the base path of your application.
https://docs.angularjs.org/api/ng/provider/$locationProvider
Also provide a .htaccess
file on your server to allways redirect the specific request to the mail index page. You might have to tweak it a bit to fit your structure.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Note that the mod_rewrite had to be enabled on my apache server. By default it was disabled.