[英]Angular 2 http change value in response not affect the page
I am quite new to angular 2. I am trying to change the value after getting response from http request of parameter that will be used in the page. 我对angular 2很新。我试图在从页面中使用的参数的http请求获得响应后更改该值。 I do not understand why it not affect at the page even though value have changed in the console log.
我不明白为什么它不会影响页面,即使控制台日志中的值已更改。
<ons-list>
<ons-list-header>Settings {{testMsg?testMsg:''}}</ons-list-header>
<ons-list-item>
<div class="center">
Enable VoLTE
</div>
<div class="right">
<ons-switch (change)="enabled()"></ons-switch>
</div>
</ons-list-item>
</ons-list>
testMsg = 'before request';
enabled() {
let body = JSON.parse(this._settingService.load(this.pageName));
let url = this._settingService.loadUrl(this.pageName);
this._sesService.getCarsRestful().subscribe(
function (response) {
console.log("Success Response" + response);
this.test2 = response;
// this.showFrame = 'show';
// console.log(this.showFrame);
this.testMsg = 'after request';
console.log(this.testMsg);
},
function (error) { console.log("Error happened" + error) },
function () {
console.log("the subscription is completed");
// console.log(this.test2[0].name);
// this.showFrame = 'show';
}
);
}
When you use function
you will lose the current this
of class
. 当你使用
function
你将失去当前this
class
。 You should use Arrow function in success
and error
function of observable. 你应该使用
success
箭头功能和可观察的error
功能。 function () {}
should be () => {}
function () {}
应该是() => {}
testMsg = 'before request';
enabled() {
let body = JSON.parse(this._settingService.load(this.pageName));
let url = this._settingService.loadUrl(this.pageName);
this._sesService.getCarsRestful().subscribe(
//changed to arrow function
(response) => {
console.log("Success Response" + response);
this.test2 = response;
// this.showFrame = 'show';
// console.log(this.showFrame);
this.testMsg = 'after request';
console.log(this.testMsg);
},
//changed to arrow function
(error) => { console.log("Error happened" + error) },
//changed to arrow function
() => {
console.log("the subscription is completed");
// console.log(this.test2[0].name);
// this.showFrame = 'show';
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.