Set reactive form after data loaded (async) - Angular 5

后端 未结 3 1716
执笔经年
执笔经年 2020-12-19 05:05

I am trying to update the form values after loading values from an API. I tried using the *ngIf technique but the form is not visible even when the form is set.

3条回答
  •  北海茫月
    2020-12-19 05:11

    i've been confronted to this issue, i don't know if my solution is the best, but it work. The technique is to use a loaded: boolean that you initiate to false and once your data fully recived in your component you set it to true

    here is an exemple :

    .html:

    loading ...

    // your template goes here

    and in your .ts:

    loaded: boolean = false;
    
    // your code ....
    
    ngOnInit() {
      setTimeout(() => {
        this._dashboardService.routeChangeStarted();
      }, 0);
      this._activatedRoute.params.subscribe(params => {
        this.news["id"] = params["id"];
        this.getPartners().then(data => {
          this.getNews().then(data=>{
            this.setForm();
    
            // here is the important part!
            this.loaded = true
          })
        });
      });
    }
    

提交回复
热议问题