Shakeer Hussain Shakeer Hussain - 3 months ago 21
AngularJS Question

Redirect to UI-Route after Login Successfully

How to redirect to Home Page after Login successful,

I am using UI-router and below is my ui-router code.

var myrouting=angular.module('routingDemoApp', ['ui.router'])
myrouting.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise("", "/index")

$stateProvider
.state('index', {
url: "/index",
templateUrl: "../Views/home.html"
})
.state('contactus', {
url: "/contactus",
templateUrl: "../Views/contactus.html",

})

.state('home', {
url: "/home",
templateUrl: "../Views/home.html",
})
.state('myModal', {
url: "/myModal",
templateUrl: "../Views/SignInPage.html",
})

}]);


I have a button element calling ng-click='login function' for validation Username and Password. once credentials are valid i want to redirect a page to "Home.html"

How to call ui-route after successfully login in below function?

var mymodule2 = angular.module('module2', []);
mymodule2.controller('controller2', function ($scope) {

$scope.loginFunction = function () {
alert("Login");
if ($scope.username == 'abcd' && $scope.password == 'hello123') {
console.log('Login sucessfull');
***// Redirect to Home Page.***

}
}

});

Answer

You can just use $state.go("home"); like this, where the "home" is the state name where you would like to go:

var mymodule2 = angular.module('module2', []);
mymodule2.controller('controller2', function ($scope, $state) {

$scope.loginFunction = function () {
  alert("Login");
    if ($scope.username == 'abcd' && $scope.password == 'hello123') {
      console.log('Login sucessfull');  
      $state.go("home");
    }
  }
});