angular 7无法在http回调中读取公共变量

问题描述 投票:0回答:1

我在我的项目中使用角度7。我的问题是我无法在http函数回调中获取公共变量。

export class TestComponent implements OnInit {
      public isFlag:boolean = false;

      constructor() { }

      ngOnInit() {
        this.getData();
      }

      getData() {
        let url = '/login.aspx';
        this.testService.getDetails(url).subscribe(
            (data) => {
            console.log('Success : ' + JSON.stringify(data));
            this.isFlag = true; // 'isFlag' variable is not in 'this' keyword. Service callback is return successfully.
        },
            (err) => { console.log('Error : ' + err); }
        );
      }
}
http callback angular7
1个回答
0
投票

请试试这个:

data: any = null;
public isFlag: boolean = false;

constructor() { }

ngOnInit() {
  this.getData();
}

getData() {
 let url = '/login.aspx';
 this.testService.getDetails(url).subscribe(
  res => { this.data = res },
  err => console.log(err),
  () => this.checkResponse(this.data)
 );
}

checkResponse(data) {
 console.log('Success : ' + JSON.stringify(data));
 this.isFlag = true;
}

希望所以它解决你的问题!

© www.soinside.com 2019 - 2024. All rights reserved.