javascript - 我不断收到 angularjs service is not a function 错误(LoginService.login 不是函数)

标签 javascript angularjs cordova ionic-framework

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

'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 文件,这对我来说很奇怪,因为我已经注入(inject)了适当的文件,但我仍然收到上述错误。任何帮助或指导将不胜感激。

'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'));
})

关于javascript - 我不断收到 angularjs service is not a function 错误(LoginService.login 不是函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36672255/

相关文章:

javascript - D3 折叠树 : Scrollable container for tree

javascript - Coderbyte 挑战 : Questions Marks - RegExp pattern '/d(\?\?\?)d/gi' incorect

javascript - 当文件太大时停止文件上传 - jQuery S3

javascript - 在创建循环的 angularjs 指令中重置范围间隔

javascript - AngularJS 如何为 Bootstrap 模式创建可重用的模板

javascript - 在另一个对象中创建一个 Javascript 对象

android - PhoneGap 的 Media 类是否支持 3gpp?

javascript - 使用Bootstrap-select返回 "$(...).data(...).on is not a function"类型错误

cordova - 将 cordova 网络插件添加到 Ionic Cordova 应用程序后出错

Android App 在 Debug模式下工作,但在 Release模式下不工作