簡體   English   中英

在父組件中,我提出一個服務請求以獲取數據。 如何告訴我的孩子組件等待響應?

[英]In my parent component, I make a service request to fetch data. How do I tell my child component to wait for a response?

我正在使用角度2。

在父組件的ngOnInit() ,我正在發出API請求以獲取一些數據:

export class ParentComponent implements OnInit {

  constructor(private myService: MyService) { }

  myData;
  errorMessage: string;

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.myService.getData()
      .subscribe(
        data => this.myData = data,
        error => this.errorMessage = <any>error
      )
  }
}

My ChildComponent將使用myData(在這種情況下,它只是帶有ID對象和名稱的名稱對象數組的JSON響應)來顯示名稱。

但是,由於我有一台希望該對象及其開頭未定義的轉發器,我如何“等待”加載響應? 現在,我從孩子那里得到一個myData是undefined錯誤。

例如在react中,我會做這樣的事情this.props.myData && DO_SOMETHING

伙計們,我是白痴。

我在中繼器周圍的標簽中做了以下操作:

<div *ngIf="myData">
  ...my repeater and other stuff
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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