解决承诺 Angular 2
Resolving Promise Angular 2
我有以下问题。
在一个函数中,我有一个 return 类型的承诺。此函数位于 class 层次结构中。
updateNodeValues(entity: String, data: {}): Promise<any>{
let jsonBody = JSON.stringify(data);
let url = environment.endpointCore + '/api/' + entity + '/' + data['id'];
return this.http.put(url, jsonBody, this.options)
.toPromise()
.then(response => {
return response;
})
.catch(this.handleError);
}
此函数在 class 节点中。
onSubmit(): void{
var currentForm = this.form.value;
var entityName = this.inflection.classify(this.node.type).toLowerCase();
var requiredData = {};
for(var i = 0; i < this.formItems.length; i++){
this.formItems[i].value = currentForm[Object.keys(currentForm)[i]];
}
for(var i=0; i<this.formItems.length; i++){
requiredData[this.globalService.camelize(this.formItems[i].label)] = this.formItems[i].value
}
Promise.resolve(this.hierarchyService.updateNodeValues(entityName, requiredData)).then(response => {
alert(response.ok);
if(response.ok){
this.globalService.showSuccessMessage('Values updated');
this.refreshGui(requiredData);
}
});
this.editMode = false;
}
问题是,当我尝试解决承诺并调用 this.refreshGui(requireddata)
时,什么也没有发生。我已经阅读了有关粗箭头如何保留此 'context' 的信息,但我不明白为什么调用此方法什么也没做,而调用 successMessage
会产生预期的结果。
我调用的方法是这样的,它也在class节点中。
private refreshGui(data: {}){
this._node.data = data;
this.objectProperties = new Array();
this.nodeChildren = new Array();
for (var property in data) {
var propertyValue = data[property];
if (propertyValue instanceof Array) {
this.nodeChildren.push({label: property, value: "total: ".concat(propertyValue.length.toString())});
} else {
this.objectProperties.push({label: property, value: propertyValue});
}
}
}
我发现可行的解决方案是实施自定义事件。问题是在异步回调解析中,this
的上下文会是 "get lost"。粗箭头使我能够用它调用 class 方法,但其中的属性将是 "lost"。由于这个原因,我从方法中提取了逻辑,并将其放在回调部分,并在一些变量中设置了预期和需要的结果。此变量已传递到我的自定义事件,并在自定义事件处理程序中适当地设置为 class 变量。
我有以下问题。
在一个函数中,我有一个 return 类型的承诺。此函数位于 class 层次结构中。
updateNodeValues(entity: String, data: {}): Promise<any>{
let jsonBody = JSON.stringify(data);
let url = environment.endpointCore + '/api/' + entity + '/' + data['id'];
return this.http.put(url, jsonBody, this.options)
.toPromise()
.then(response => {
return response;
})
.catch(this.handleError);
}
此函数在 class 节点中。
onSubmit(): void{
var currentForm = this.form.value;
var entityName = this.inflection.classify(this.node.type).toLowerCase();
var requiredData = {};
for(var i = 0; i < this.formItems.length; i++){
this.formItems[i].value = currentForm[Object.keys(currentForm)[i]];
}
for(var i=0; i<this.formItems.length; i++){
requiredData[this.globalService.camelize(this.formItems[i].label)] = this.formItems[i].value
}
Promise.resolve(this.hierarchyService.updateNodeValues(entityName, requiredData)).then(response => {
alert(response.ok);
if(response.ok){
this.globalService.showSuccessMessage('Values updated');
this.refreshGui(requiredData);
}
});
this.editMode = false;
}
问题是,当我尝试解决承诺并调用 this.refreshGui(requireddata)
时,什么也没有发生。我已经阅读了有关粗箭头如何保留此 'context' 的信息,但我不明白为什么调用此方法什么也没做,而调用 successMessage
会产生预期的结果。
我调用的方法是这样的,它也在class节点中。
private refreshGui(data: {}){
this._node.data = data;
this.objectProperties = new Array();
this.nodeChildren = new Array();
for (var property in data) {
var propertyValue = data[property];
if (propertyValue instanceof Array) {
this.nodeChildren.push({label: property, value: "total: ".concat(propertyValue.length.toString())});
} else {
this.objectProperties.push({label: property, value: propertyValue});
}
}
}
我发现可行的解决方案是实施自定义事件。问题是在异步回调解析中,this
的上下文会是 "get lost"。粗箭头使我能够用它调用 class 方法,但其中的属性将是 "lost"。由于这个原因,我从方法中提取了逻辑,并将其放在回调部分,并在一些变量中设置了预期和需要的结果。此变量已传递到我的自定义事件,并在自定义事件处理程序中适当地设置为 class 变量。