Angularjs $http 然后函数
Angularjs $http then function
我已经创建了一个 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;
}
};
var http
是一个承诺对象,因为 Angular 的 $http
服务的 return 值是一个承诺 (docs)。一旦 AJAX 请求已 returned 且承诺已解决,请使用 .then()
获取 return 值。
var self = this;
http.then(function (data) {
self.user = data;
});
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;
});
}
};
将其分配给不同的值或!或者我们 .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));
我已经创建了一个 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;
}
};
var http
是一个承诺对象,因为 Angular 的 $http
服务的 return 值是一个承诺 (docs)。一旦 AJAX 请求已 returned 且承诺已解决,请使用 .then()
获取 return 值。
var self = this;
http.then(function (data) {
self.user = data;
});
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;
});
}
};
将其分配给不同的值或!或者我们 .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));