Angular - Wait until I receive data before loading template

前端 未结 3 1017
野的像风
野的像风 2020-12-23 17:20

So, I have a component that renders several components dynamically, with this template:

3条回答
  •  长情又很酷
    2020-12-23 17:34

    {{homeData?.meta[0].site_desc}}

    Just Used a "?" after the variable that has been getting loaded with data from the server.

    home.component.ts

    import { Component, OnInit } from '@angular/core';
    import { HomeService } from '../services/home.service';
    
    @Component({
      selector: 'app-home',
      templateUrl: './home.component.html',
      styleUrls: ['./home.component.scss']
    })
    export class HomeComponent implements OnInit {
      public homeData: any;
      constructor(private homeService: HomeService) {}
    
      ngOnInit(): void {
        this.homeService.getHomeData().subscribe( data => {
          this.homeData = data[0];
        }, error => {
          console.log(error);
        });
      }
    }
    

提交回复
热议问题