我一直在获取angularjs服务不是函数错误(LoginService.login不是函数)

I keep getting angularjs service is not a function Error( LoginService.login is not a function)

本文关键字:函数 LoginService login 错误 angularjs 我一直在 获取 服务      更新时间:2023-09-26

这是我的LoginController,正如你所看到的,我已经注入了LoginService,我似乎不明白为什么我会出现上面提到的错误(注意:我通过将我的项目分解到单独的文件夹中,使用gulp和browserfy将所有内容捆绑到一个文件中,使项目模块化)

'use strict';
function LoginController($scope, $ionicModal, $timeout, $location,
                         $ionicLoading, $ionicPopup, LoginService) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //$scope.$on('$ionicView.enter', function(e) {
  //});
  // Form data for the login modal
  $scope.loginData = {};
  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('js/modules/login/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });
  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };
  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };
  $scope.show = function() {
    $ionicLoading.show({
      template:'<p>Loading...</p><ion-spinner></ion-spinner>'
    });
  };
 $scope.hide = function(){
   $ionicLoading.hide();
 };
  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);
    // Start showing the progress
    $scope.show($ionicLoading);

    // Do the call to a service using $http or directly do the call here
    LoginService.login($scope.loginData).success(function(data) {
      // Do something on success for example if you are doing a login
        console.log('Login successful', data);
    }).error(function(data) {
      // Do something on error
      console.log('Login failed', data);

    }).finally(function($ionicLoading) {
      // On both cases hide the loading
      console.log('Hide');
      $scope.hide($ionicLoading);
    });
  };
}
module.exports = ['$scope', '$ionicModal', '$timeout','$location',
                  '$ionicLoading','LoginService','$ionicPopup',
                   LoginController];

这是我的LoginService文件,这对我来说很奇怪,因为我已经注入了合适的文件,但我仍然收到上面提到的错误。如有任何帮助或指导,我们将不胜感激。

'use strict';
function LoginService($http, $q, API_ENDPOINT) {
  var BASE_URL = API_ENDPOINT.url;
  var LOCAL_TOKEN_KEY = 'yourTokenKey';
  var isAuthenticated = false;
  var authToken;

  function storeUserCredentials(token) {
    window.localStorage.setItem(LOCAL_TOKEN_KEY, token);
    useCredentials(token);
  }
  function useCredentials(token) {
    isAuthenticated = true;
    authToken = token;
    // Set the token as header for your requests!x-access-token'
    $http.defaults.headers.common.Authorization = authToken;
  }
  var login = function(user) {
    return $q(function(resolve, reject) {
      $http.post(BASE_URL + '/authenticate', user).then(function(result){
        if (result.data.success) {
          storeUserCredentials(result.data.token);
          resolve(result.data.msg);
        }else{
          reject(result.data.msg);
        }
      });
    });
  };
  return {
    login: login,
    isAuthenticated: function() {return isAuthenticated;},
  };

}
module.exports = ['$http', '$q', 'API_ENDPOINT', LoginService];

这是我的login.js文件,与上发布的文件位于同一目录中

'use strict';
module.exports = angular.module('login', [])
    .factory('LoginService', require('./login-service'))
    .controller('LoginController', require('./login-controller'));

您应该先执行require,然后只定义服务。由于直接在.factory('LoginServie中传递require,require()`服务名称正在注册,但其正文为空。

我对require的工作不多,但这里是你可以尝试的:

require('./login-service');
module.exports = angular.module('login', [])
    // Make this code synchornous that this should only run when above require loaded the script
    .factory('LoginService', LoginService)
    .controller('LoginController', require('./login-controller'));

或者(可能)

require('./login-service', function() {
    module.exports = angular.module('login', [])
        .factory('LoginService', LoginService)
        .controller('LoginController', require('./login-controller'));
})

当使用工厂时,您得到的是实际的类,您需要实例化它。当使用服务时,您会得到服务的实例。看看下面的例子:AngularJS:工厂和服务?