簡體   English   中英

響應中的Angular 2 http更改值不會影響頁面

[英]Angular 2 http change value in response not affect the page

我對angular 2很新。我試圖在從頁面中使用的參數的http請求獲得響應后更改該值。 我不明白為什么它不會影響頁面,即使控制台日志中的值已更改。

page.html中

<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>

page.ts

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';
        }
    );

}

當你使用function你將失去當前this class 你應該使用success箭頭功能和可觀察的error功能。 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM