无法设置在$ HTTP回调的一类变种 [英] Unable to set a class var in $http callback

查看:82
本文介绍了无法设置在$ HTTP回调的一类变种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,我试图设置在下面的服务定义的变量,在成功回调。我无法设置VAR authMap的服务。这里发生了什么?我该怎么办呢?

  app.service(授权,['$ HTTP',函数($ HTTP){
  this.authMap = [];
  。$ http.get('/ authmap')成功(功能(数据){this.authMap =数据});}]);


解决方案

一个新的范围在您的匿名回调函数创建的,所以这个是不同的。结果
你可以这样做:

  app.service(授权,['$ HTTP',函数($ HTTP){
  this.authMap = [];
  VAR认为这=;
  。$ http.get('/ authmap')成功(功能(数据){that.authMap =数据});
}]);

In the example below I am trying to set a variable defined in the below service, in the success callback. I am unable to set the var authMap in the service. What is happening here and how do I do it?

app.service("authorization", ['$http', function($http){
  this.authMap = [];
  $http.get('/authmap').success(function(data){this.authMap = data});

}]);

解决方案

A new scope is created in your anonymous callback function, so this is different.
You could do something like:

app.service("authorization", ['$http', function($http){
  this.authMap = [];
  var that = this;
  $http.get('/authmap').success(function(data){ that.authMap = data });
}]);

这篇关于无法设置在$ HTTP回调的一类变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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