2017-04-12 123 views
0

我已經使用angularJS創建了一個登錄頁面,我在提交表單時將用戶名和密碼設置爲靜態,並將其重定向到歡迎屏幕,現在我希望在單擊註銷時註銷,從歡迎頁面註銷並返回到登錄頁面?怎麼可能使用angularJS?如何使用angularJS從頁面註銷

這裏是我寫的代碼:

.controller("loginController", function($scope, $location, $rootScope){ 
          $scope.login = function() { 
           var user = $scope.username; 
           var password = $scope.password; 
           var result = $scope.username + $scope.password; 
           console.log(result); 
           if (user == "admin" && password == 'admin'){ 
            $rootScope.loggedIn = true; 
            $location.path('/welcome'); 
           } else { 
            alert("INVALID CREDENTIALS"); 
           } 
          } 
.controller('welcomeController', function($scope){ 
        $scope.message = "welcome here" 
       }) 
+0

我也將用戶和密碼存儲在一個變量中,但不知道它是如何工作的! –

回答

0

寫一個函數到您的網頁重定向到主頁,做到這一點,

.controller('logoutController', function($scope,$location){ 
    $scope.logout = function(){ 
     $location.path('/home'); 
    } 
}) 

,並使用稱之爲NG單擊

<button ng-click="logout()"> 
    Logout 
</button> 
+0

Thankyou它的工作 –

+0

@kritikaverma標記爲答案,如果它有幫助 – Sajeetharan

+0

你在這裏做的是重定向到主頁,但如果我更改像http:// localhost/login /#/的URL到http://本地主機/登錄/#/歡迎然後它重新導向我的歡迎頁面,在這種情況下不會發生,如果我沒有登錄!使用這種方法,這種方法是錯誤的,必須有一些條件需要放! –

1

按照前answer中的Sajeetharan所述進行。
在註銷功能

$scope.username$scope.password只是清晰的價值觀寫一個函數到您的網頁重定向到主頁,做到這一點,

.controller('logoutController', function($scope,$location){ 
    $scope.logout = function(){ 
     //Just clear values from scope 
     $scope.username = ''; 
     $scope.password = ''; 
     $location.path('/home'); 
    } 
}) 

,並使用稱之爲NG單擊

<button ng-click="logout()"> 
    Logout 
</button> 

順便說一句,您使用的方法不是很好。

+0

當前我正在使用靜態輸入值,因爲我是AngularJS的初學者,我的問題不是我需要我的主頁的值不會在歡迎屏幕上重定向,除非除非用戶登錄。在我的情況下,如果我將url localhost/login /#更改爲localhost/login /#/ welcome,那麼它會重定向到此頁面,無論我是否登錄了! –

+0

您必須在控制器或服務中創建一個**驗證**函數(_better option_),您必須調用任何控制器的加載函數(_running該函數首先應該在controller_中執行)。在該功能中,您必須檢查用戶是否有權訪問當前頁面並採取相應措施。 – Chirag

相關問題