2016-03-29 18 views
4

Açısal js konusunda yeniyim ve ng-view ve ngRoute yönergelerinde çalışıyorum.Açısal js'de denetleyici görünümü nasıl değiştirilir?

Ben şöyle giriş düğmesi için kod

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

şöyle yazılı ve loginController içinde giriş işlevi yürütür alacak butonuna tıklayarak ilgili ve benim denetleyicisi yazılır sahip olduğu bir loginPage.html vardır:

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=""; 
     }); 
    }; 

}]); 

response.status==true sonra /home görünümümü değiştirmek istiyorum. Birisi bana bunu nasıl yapacağımı söyleyebilir mi?

Teşekkür ederiz.

cevap

1
if(response.status == "true") 
    $location.path("/home"); 

böyle Denetleyiciniz parametrelerine $location eklemeyi unutmayın :

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

Ayrıca, yalnızca ilk satır deyimi içinde olduğunu başka parantez içinde else deyimi kaydırmak veya:

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

sayesinde, işe yaradı! $ lokasyonunu kontrolör parametrelerine eklemeyi unuttum – Minions

1

Kullanım $location.path: denetleyici dependancy listesinde $location ekleyerek kaçırmış Ayrıca

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

:

function($scope, $http, $location) 
0
if(response.status == "true") 
    $location.path("/home"); 
İlgili konular