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

时间:2019-02-19 06:06:39

标签: http callback angular7

我在项目中使用的是角度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); }
        );
      }
}

1 个答案:

答案 0 :(得分: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;
}

希望如此可以解决您的问题!