How to change view from controller in angular js?

I am new to angular js and trying to work with directives ng-viewand ngRoute.

I have loginPage.html in which I have login code written as follows

<button class="button button-block" ng-click="login()">Log In</button> 

and when I click the button, the login function in loginController will be executed, and my controller will be written as follows:

var App = angular.module('App', ['ngRoute']);

App.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'loginPage.html',
        controller: 'loginController'
      }).
      when('/home', {
        templateUrl: 'homePage.html',
        controller: 'homeController'
      });
  }]);

App.controller('loginController', ['$scope', '$http', '$location', function($scope, $http) {
    console.log("Hello from login controller");


    $scope.login = function() {
        //console.log($scope.user);
        $http.post('/login', $scope.user).success(function(response) {
            if(response.status == "true")
                //if the response is true i want to go to homepage.html.

            else
                $scope.error="Invalid login! Please try again";
                $scope.user.email="";
                $scope.user.password="";
      });
    };

}]);

If response.status==true, then I want to change my view to /home. Can someone please tell me how to do this?

Thank.

+4
source share
3 answers
if(response.status == "true")
    $location.path("/home");

$location :

['$scope', '$http', '$location', function($scope, $http, $location)...

, else , :

else {
    $scope.error="Invalid login! Please try again";
    $scope.user.email="";
    $scope.user.password="";
}
+1

$location.path:

if(response.status == "true") {
    $location.path('/home');
}

, $location :

function($scope, $http, $location)
+1
if(response.status == "true")
   $location.path("/home");
0
source

All Articles