角注射服务(异步)不工作 [英] Angular service injection (async) not working

查看:108
本文介绍了角注射服务(异步)不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个服务使用存储在一个服务数据。

问题:数据检索的回调函数似乎是异步的,我不能申请我的方法检索到的数据。

看起来什么数据检索像

  app.factory('userService',['$ HTTP,rootUserService',函数($ HTTP,rootUserService){
  变种用户=无效;
  VAR rootUser;  返回{
    的getUser:功能(用户id){
      VAR认为这=;      rootUserService.getUser()。然后(功能(数据){
        that.rootUser =数据;
        的console.log(数据)//工作
      });
      的console.log(that.rootUser)//未定义
      的console.log(rootUser)//未定义      //做一些东西与rootuser(另一种异步调用和放大器;比较用户)
    }
  }
}]);

当数据从检索:

  app.factory('rootUserService',['$ HTTP,$ rootScope',函数($ HTTP,$ rootScope){
  变种用户=无效;
  返回{
    makeRequest的:功能(URL){
      VAR认为这=;
      返回$ http.get(URL)。然后(功能(RES){
        返回res.data;
      });
    },    //这是被用于检索
    的getUser:功能(){
      如果(this.user!){this.user = this.makeRequest('/ API /用户/获取'); };
      返回this.user;
    }
  }
}]);


解决方案

在服务的getUser()方法应该返回来解决的承诺。事情是这样的:

  app.factory('rootUserService',['$ HTTP,$ rootScope',函数($ HTTP,$ rootScope){
  变种用户=无效;
  返回{
    makeRequest的:功能(URL){
      VAR认为这=;
      返回$ http.get(URL)。然后(功能(RES){
        返回res.data;
      });
    },    //这是被用于检索
    的getUser:功能(){
      如果(!this.user){
       返回this.makeRequest('/ API /用户/获取'),然后(功能(RESP){
           this.user =数据;
           返回的数据;
        });
      };
      返回this.user;
    }
  }
}]);app.factory('userService',['$ HTTP,rootUserService',函数($ HTTP,rootUserService){
  变种用户=无效;
  VAR rootUser;  返回{
    的getUser:功能(用户id){
      VAR认为这=;      返回rootUserService.getUser()。然后(功能(数据){
        that.rootUser =数据;
        的console.log(数据)//工作
        //做一些东西与rootuser(另一种异步调用和放大器;比较用户)
        //所有这些行动都必须在这里
      });
    }
  }
}]);

I'm want to use data stored in one service in another service.

Problem: The callback function for data retrieval seems to be async, and I can't apply my methods for the retrieved data.

What data retrieval looks like

app.factory('userService', ['$http', 'rootUserService', function ($http, rootUserService) {
  var user = null;
  var rootUser;

  return {
    getUser : function(userId) {
      var that = this;

      rootUserService.getUser().then(function(data) {
        that.rootUser = data;
        console.log(data) // working
      });
      console.log(that.rootUser) // undefined
      console.log(rootUser) // undefined

      // Do some stuff with rootuser (another async call & compare users)
    }
  }
}]);

Where data is retrieved from:

app.factory('rootUserService', ['$http', '$rootScope', function($http, $rootScope) {
  var user = null;
  return {
    makeRequest: function(url) {
      var that = this;
      return $http.get(url).then(function (res) {
        return res.data;
      });
    },

    // This is what is used for retrieval
    getUser: function() {
      if(!this.user) { this.user = this.makeRequest('/api/user/get'); };
      return this.user;
    }
  }
}]);

解决方案

The getUser() method in the service should return the promise to resolve. Something like this:

app.factory('rootUserService', ['$http', '$rootScope', function($http, $rootScope) {
  var user = null;
  return {
    makeRequest: function(url) {
      var that = this;
      return $http.get(url).then(function (res) {
        return res.data;
      });
    },

    // This is what is used for retrieval
    getUser: function() {
      if(!this.user) { 
       return this.makeRequest('/api/user/get').then(function(resp) {
           this.user=data;
           return data;
        }); 
      };
      return this.user;
    }
  }
}]);

app.factory('userService', ['$http', 'rootUserService', function ($http, rootUserService) {
  var user = null;
  var rootUser;

  return {
    getUser : function(userId) {
      var that = this;

      return rootUserService.getUser().then(function(data) {
        that.rootUser = data;
        console.log(data) // working
        // Do some stuff with rootuser (another async call & compare users)
        //All of those actions MUST BE IN HERE
      });
    }
  }
}]);

这篇关于角注射服务(异步)不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆