2017-10-11 58 views
0

我在登錄控制器兩種功能兩個功能都 實現登錄目的, 在這個控制器!scope.isLoggedIn條件satistified需要檢查裏面的狀態。 我需要永久登錄在應用程序,所以我存儲用戶名和密碼credientical在本地sessionStorage 只要值在localStorage中可用我需要執行自動登錄功能, 我檢查localStorage數據可用或不在if條件 如果用戶名和密碼是localStorage中可用我需要執行automaticLoginUser函數 如果不是無需執行自動登錄功能 每當我嘗試執行該automaticLoginUser函數獲取錯誤 TypeError:scope.automaticLoginUser不是函數錯誤。 謝謝先進..!如何調用內部的控制器功能於angularjs

app.controller('LoginCtrl', ['$scope', 
     'userService', '$state', '$rootScope','BackendService', 'CartService', 

     function(scope, userService, $state, rootScope, BackendService, CartService) { 
      scope.user = {}; 
      scope.isLoggedIn = false; 
      scope.userId = CartService.getuserId(); 
      scope.userPassword = CartService.getuserPassword(); 
      if (!scope.isLoggedIn) { 
       console.log('in side isLoggedIn'); 
       if (scope.userId !== undefined && scope.userPassword !== undefined) { 
        var loginId = scope.userId; 
        var password = scope.userPassword; 
        scope.user.loginId = loginId; 
        scope.user.password = password; 
        console.log('after user' + scope.user); 
        var user = scope.user; 
        scope.automaticLoginuser(user); 
       } 
       scope.automaticLoginuser = function(user) { 
        alert("Inside automaticLoginuser"); 
        CartService.saveuserId(scope.user.loginId); 
        CartService.saveuserPassword(scope.user.password); 

        userService.loginuser(user) 
         .then(function(response) { 
          scope.userUuid = response.data.userUuid; 
          userService.setuserUuid(scope.userUuid); 

          if (response.data.status === 'success') { 
           CartService.saveFuuid(scope.fuuid); 
           $state.go("app.userHome"); 
          } else { 
           $state.go("app.login"); 
          } 
         }); 
       }; 

       scope.loginuser = function(user) { 
        CartService.saveuserId(scope.user.loginId); 
        CartService.saveuserPassword(scope.user.password); 

        userService.loginuser(user) 
         .then(function(response) { 
          scope.userUuid = response.data.userUuid; 
          userService.setuserUuid(scope.userUuid); 

          if (response.data.status === 'success') { 
           $state.go("app.userHome"); 
          } else { 
           $state.go("app.login"); 

          } 
         }); 
       }; 
      } 
     ]); 
+1

我想你是在調用scope.automaticLoginuser(user);在宣佈之前 – Gerard

回答

0

scope.automaticLoginuser正在if語句之後定義。首先要做的是在控制器中定義更高的方法。

2

首先我不能誇大其詞,建議在前端的任何位置保存用戶名和密碼,包括本地會話存儲。希望你以某種方式對它進行散列。

其次,您面臨的問題是因爲您正試圖在聲明之前在控制器內調用範圍。無論如何,這是不必要的,因爲$ scope是這個角度實例化的控制器的實例,您可以從DOM調用它。

所以正確的事情是定義這個函數,因爲你只打算在你的控制器中調用它。

function automaticLoginuser(user) { 
    alert("Inside automaticLoginuser"); 
    CartService.saveuserId(scope.user.loginId); 
    CartService.saveuserPassword(scope.user.password); 

    userService.loginuser(user) 
     .then(function(response) { 
      scope.userUuid = response.data.userUuid; 
      userService.setuserUuid(scope.userUuid); 

      if (response.data.status === 'success') { 
       CartService.saveFuuid(scope.fuuid); 
       $state.go("app.userHome"); 
      } else { 
       $state.go("app.login"); 
      } 
     }); 
}; 

然後叫它正常

automaticLoginuser(user); 
0

你只需要重新排序的功能。

var app = angular.module("myApp", []); 
 
app.controller('LoginCtrl', ['$scope', 
 
    /*'userService', '$state', '$rootScope','BackendService', 'CartService',*/ 
 

 
    function(scope /*, userService, $state, rootScope, BackendService, CartService*/) { 
 

 
    scope.automaticLoginuser = function(user) { 
 
     alert("Inside automaticLoginuser"); 
 
     /*CartService.saveuserId(scope.user.loginId); 
 
     CartService.saveuserPassword(scope.user.password); 
 

 
     userService.loginuser(user) 
 
     .then(function(response) { 
 
      scope.userUuid = response.data.userUuid; 
 
      userService.setuserUuid(scope.userUuid); 
 

 
      if (response.data.status === 'success') { 
 
      CartService.saveFuuid(scope.fuuid); 
 
      $state.go("app.userHome"); 
 
      } else { 
 
      $state.go("app.login"); 
 
      } 
 
     });*/ 
 
    }; 
 

 
    scope.loginuser = function(user) { 
 
     /*CartService.saveuserId(scope.user.loginId); 
 
     CartService.saveuserPassword(scope.user.password); 
 

 
     userService.loginuser(user) 
 
     .then(function(response) { 
 
      scope.userUuid = response.data.userUuid; 
 
      userService.setuserUuid(scope.userUuid); 
 

 
      if (response.data.status === 'success') { 
 
      $state.go("app.userHome"); 
 
      } else { 
 
      $state.go("app.login"); 
 

 
      } 
 
     });*/ 
 
    }; 
 
    scope.user = {}; 
 
    scope.isLoggedIn = false; 
 
    scope.userId = 'admin'; //CartService.getuserId(); 
 
    scope.userPassword = 'admin'; //CartService.getuserPassword(); 
 
    if (!scope.isLoggedIn) { 
 
     console.log('in side isLoggedIn'); 
 
     if (scope.userId !== undefined && scope.userPassword !== undefined) { 
 
     var loginId = scope.userId; 
 
     var password = scope.userPassword; 
 
     scope.user.loginId = loginId; 
 
     scope.user.password = password; 
 
     console.log('after user' + scope.user); 
 
     var user = scope.user; 
 
     scope.automaticLoginuser(user); 
 
     } 
 
    } 
 
    } 
 
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="LoginCtrl"> 
 
</div>

控制器真的沒有 「構造」 - 他們通常使用的一樣的功能。但你可以將初始化放在你的控制器函數中,它會在開始時執行,就像構造函數一樣:

function exampleController($scope) { 
    $scope.firstMethod = function() { 
    //initialize the sampleArray 
    }; 

    $scope.secondMethod = function() { 
    $scope.firstMethod(); 
    }; 

    $scope.firstMethod(); 
} 
相關問題