【问题标题】:Angularjs $http then functionAngularjs $http 然后函数
【发布时间】:2015-04-22 10:26:29
【问题描述】:

我创建了一个 JS 对象,其方法调用 $http 来检索一个值,但是当 $http 完成后我想将此值分配给一个属性,我似乎无法获得这个值:

属性 this.user 总是以承诺本身结束,但我想分配从 XHR 请求返回的值或失败时未定义的值,我认为这是一个上下文问题,我只是不知道如何解决它

var Ticket = function(numint, company_id, user_id, title, description, priority, status, assignation, created_at) {
            this.numint         = numint;
            this.company_id     = company_id;
            this.user_id        = user_id;
            this.title          = title;
            this.description    = description;
            this.priority       = priority;
            this.status         = status;
            this.assignation    = assignation;
            this.created_at     = created_at;

            this.user           = undefined;

            this.getUser = function() {

                if(this.user_id === undefined)
                    return false;

                var http = 
                    $http({
                        method  : 'GET',
                        url     : '/users/' + this.user_id,
                        timeout : 100000,
                        headers : {'Content-Type': 'application/x-www-form-urlencoded'}
                    });

                this.user = http
                    .then(
                        function(data) {
                            return data;
                        }
                        , 
                        function() {
                            return undefined;
                        });

                return http;

            }
        };

【问题讨论】:

  • $http 是一个异步操作 - 你不能 return 来自成功处理程序的值并分配它 this.user - 你必须在处理程序内部分配它:self.user = data; 并分配var self = this;事先
  • 是的,虽然是这样,但是我如何在处理程序中引用 this.user ???如果我输入 this.user... 上下文错误,则 this 指向处理程序

标签: angularjs http promise


【解决方案1】:

将其分配给不同的值或!或者我们.bind。

$http({
  method  : 'GET',
  url     : '/users/' + this.user_id,
  timeout : 100000,
  headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (data){
  this.user = data;
}.bind(this));

【讨论】:

    【解决方案2】:

    var http 是一个 promise 对象,因为 Angular 的 $http 服务的返回值是一个 promise (docs)。在 AJAX 请求返回且 promise 已解决后,使用 .then() 获取返回值。

    var self = this;
    
    http.then(function (data) {
      self.user = data;
    });
    

    【讨论】:

    • 我试过了,不行,this指向的是函数处理程序,而不是我的对象
    • @GabrielMatusevich,我编辑了 Morgan 的问题以修复 this 参考
    • 略有不同。而不是我使用 .success 所以数据是实际数据。否则我不得不做 data.data
    • @GabrielMatusevich 可以使用 .success() 来返回数据,而不是 .then() 来返回整个响应对象。请记住 .success() 和 .error() 是 Angular 的 $http 和 $resource 服务独有的便利函数。因此,如果您更改了代码以使承诺不会从 $http 返回(例如从缓存中加载数据),那么 .success() 和 .error() 将不再存在。 .success()、.error() 和 .then() 之间的区别更深入的解释可以找到here
    【解决方案3】:
    var Ticket = function(numint, company_id, user_id, title, description, priority, status, assignation, created_at) {
            var self = this; // new code
            self.numint         = numint; //use self inseat
            self.company_id     = company_id;
    
            this.getUser = function() {
    
                if(self.user_id === undefined) // you had one preblem here, because "this" here is direrent to the this.user_id you neded, so use self
                    return false;
    
                var http = 
                    $http({
                        method  : 'GET',
                        url     : '/users/' + this.user_id,
                        timeout : 100000,
                        headers : {'Content-Type': 'application/x-www-form-urlencoded'}
                    }).then(function (data){
                          self.user = data
                       ;},
                     function () {
                      self.user= undefined;
                       });
    
    
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      • 2015-03-01
      • 2015-11-17
      相关资源
      最近更新 更多