如何在angularjs中调用控制器内部的函数

时间:2017-10-11 10:52:49

标签: javascript angularjs ionic-framework

我在登录控制器中有两个功能,两个功能都是      实现登录目的,      在此控制器!scope.isLoggedIn 条件满意时需要检查内部情况。       我需要在应用程序中进行登录,因此我将用户ID和密码存储在本地sessionStorage中       只要localStorage中有值,我就需要执行automaticLogin函数,       我检查localStorage数据是否可用如果条件       如果localStorage中都有userid和password,我需要执行automaticLoginUser函数       如果不是不需要执行automaticLogin功能       每当我尝试执行该automaticLoginUser函数获取错误       TypeError:scope.automaticLoginUser不是函数错误。       谢谢你的高级..!

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

                            }
                        });
                };
            }
        ]);

3 个答案:

答案 0 :(得分:2)

首先,我不能夸大在前端的任何地方(包括本地会话存储)保存用户用户名和密码是多么不明智。希望你能以某种方式对它进行深入研究。

其次,您面临的问题是因为您在声明范围之前尝试在控制器内调用范围。无论如何这都是不必要的,因为$ scope是一个这样的实例,其中angular实例化了控制器,你可以从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);

答案 1 :(得分:0)

在你的if语句之后定义了

scope.automaticLoginuser。首先要做的是在控制器中定义更高的方法。

答案 2 :(得分: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();
}