在Angular JS服务中返回数据的目的是什么?

时间:2016-03-19 10:59:47

标签: angularjs

我开始使用角度JS,在我使用的服务中有一些我不太懂的东西:

(function () {

  angular
    .module('meanApp') // service qui dépend de ce module ?
    .factory('authentication', authentication);

  // $inject : To allow the minifiers to rename the function parameters and still be able to inject the right services, the function needs to be annotated with the $inject property. The $inject property is an array of service names to inject.
  // https://docs.angularjs.org/guide/di

  authentication.$inject = ['$http', '$window'];

  function authentication ($http, $window) {

    console.log("enters authentication service");
    var saveToken = function (token) {
      $window.localStorage['mean-token'] = token;
    };

    var getToken = function () {
      return $window.localStorage['mean-token'];
    };

    var isLoggedIn = function() {
      var token = getToken();
      var payload;

      if(token){
        payload = token.split('.')[1];
        payload = $window.atob(payload); //will decode a Base64 string
        payload = JSON.parse(payload);

        return payload.exp > Date.now() / 1000;
      } else {
        return false;
      }
    };

    var currentUser = function() {
      if(isLoggedIn()){
        var token = getToken();
        var payload = token.split('.')[1];
        payload = $window.atob(payload);
        payload = JSON.parse(payload);
        return {
          email : payload.email,
          name : payload.name
        };
      }
    };

    //An interface between the Angular app and the API, to call the login and register end-points and save the returned token. This will use the Angular $http service


    var register = function(user) {
      console.log("ARNAUD: Arriving in register promise");
      return $http.post('/api/register', user).success(function(data){
        saveToken(data.token);
      });
    };

    var login = function(user) {
      return $http.post('/api/login', user).success(function(data) {
        saveToken(data.token);
      });
    };

    var logout = function() {
      $window.localStorage.removeItem('mean-token');
    };


   // must return an object or function or at least a value from the factory
    return {
      currentUser : currentUser,
      saveToken : saveToken,
      getToken : getToken,
      isLoggedIn : isLoggedIn,
      register : register,
      login : login,
      logout : logout
    };

  }


})();

上面声明的函数已经返回了我需要的东西,我只需要在我的控制器中调用它们:

authentication.register(vm.credentials)

的确切目的是什么?
return {
  currentUser : currentUser,
  saveToken : saveToken,
  getToken : getToken,
  isLoggedIn : isLoggedIn,
  register : register,
  login : login,
  logout : logout
};

对于有角度的JS高级开发人员来说,这可能是一个愚蠢的问题,但对我来说并不清楚

感谢您帮我详细说明

2 个答案:

答案 0 :(得分:0)

工厂是一个由angular调用的函数,它应该返回您想要为应用程序提供的服务实例。

如果你没有从工厂返回任何东西,它将有效地返回undefined,每次你在控制器,指令等中注入authentication服务,你将得到未定义,而不是用方法获取对象。

答案 1 :(得分:0)

module.factory()将返回一个对象 - >为了能够在该对象上调用任何内容,您必须将其返回。

另一种方法是使用module.service - 这将返回一个实例。

module.service('test', function() {
  this.nice = function () {};
});

module.factory('test', function() {
  function something() {}
  return {
    nice: something
  };
});

工厂比服务更强大 - 想象如下:

module.fatory('prefix', function() {
  return function (prefix) {
    return {
      log: function (message) {
        console.log(prefix+': '+message;
      }
    };
  };
});