角度服务呼叫两次

时间:2016-07-01 09:45:54

标签: angularjs angular-services angular-controller

我几个小时就遇到了这个问题,并且为了解决这个问题而奋斗,

所以我在这里发布了一些我的文件,

这是我的index.html

<body ng-app="app" ng-class="!$auth.isAuthenticated()? 'page-header-fixed page-sidebar-closed-hide-logo page-content-white' : 'login'" >


  <!-- NAVIGATION -->
  <div data-ng-include="'app/views/common/header/header.html'" style="width:100%;"></div>
   <div class="clearfix"> </div>
  <!-- MAIN CONTENT -->
<div class="page-container">
  <div data-ng-include="'app/views/common/sidebar/sidebar.html'"></div>
   <div class="page-content-wrapper">
     <div ui-view></div>
   </div>
 </div>

 <div data-ng-include="'app/views/common/footer/footer.html'" class="page-footer" style="bottom:0;"></div>
  <script src="app/scripts/login/services/login.service.js"></script>
   <script src="app/scripts/login/controllers/login.controller.js">       </script>
   <script src="app/scripts/common/services/local.storage.js"></script>
  <script src="app/scripts/common/services/authentication.service.js"></script>

就是说,我已经添加了登录控制器,服务,本地存储和身份验证js文件

这是我的控制器

angular.module('app').controller('loginController',['loginService', 'toastr', '$state', 'authenticationService',function (loginService, toastr, $state, authenticationService) {


var vm = this;

vm.login = function (userModel) {

    loginService.login(userModel, function (response) {

        if (response.status === 200) {
             authenticationService.setCredentials(response.data);
            $state.go('companyList');
            return;
        } 
        if(response.status === 400) {
            return toastr.error("login Failed");
        }
    });
}

}]);

我的服务如下,

 (function () {    
'use strict';

angular.module('app').factory('loginService', loginService);

loginService.$inject = ['httpi', 'API','$localStorage'];

function loginService(httpi, API, $localStorage) {

    var service = {};

    service.login = login;
    service.setUserDetails = setUserDetails;
    service.getUserDetails = getUserDetails;


    return service;

    function login(user, callback) {

        httpi({
            method:"post",
            url: API.login,
            data: {
                email:user.email,
                password:user.password
            }
        }).then(function (response) {
           callback(response);
        }, function (response) {
            callback(response);
        });
    }

    function setUserDetails(data) {
        $localStorage.setObject("userData", data);
    }

    function getUserDetails() {
        return $localStorage.getObject("userData");
    }

}
})();
问题是,这个Login服务被调用了两次。 供参考。我经历了类似的问题,我没有添加控制器两次,角度文件两次,ui-view两次,服务两次,我没有ng-show,ng-hide东西

所以我找不到解决方案,

我不知道这个服务被调用了两次。我已经验证了authentication.js,我也试过使用开发人员工具,我找不到解决方案。

由于我的服务被调用了两次,因此该控制器的依赖项toastr也会被调用两次。

从很久以后来到这里,所以有人请帮我解决这个问题。

如果我的结果需要其他任何内容,我很乐意为您提供

1 个答案:

答案 0 :(得分:1)

我找到了解决方法,

我不知道按钮类型问题

在我的html表单中,我添加了ng-submit,并且在相同的表单 - 操作按钮类型中提交,我使用ng-click提交表单,

我后来发现这是错误的,

如果我们使用按钮类型作为按钮,那么ng-click工作,如果按钮类型是提交,那么你不需要按钮单击该按钮。

要学习很多东西,喜欢编码。

希望它可能有所帮助,有些人......