簡體   English   中英

Angular - 從 api 加載后在模板中顯示數據

[英]Angular - Show data in template after load from api

我有一個服務 http,它向 API 加載(和放置)數據。 在這種情況下,我需要從服務器獲取數據並在模板中顯示它們。

這是我的代碼

export class RouteDetailComponent implements OnInit{
  public routeId:number;
  public route:Route;
  public actUrl: string;
ngOnInit() {
this.routeId = this.activateDroute.snapshot.params['id'];

let data;
this.httpService.getRoute(this.routeId)
    .subscribe(
        (response: Response) => {
            data = response.json().data;

            this.route = new Route(
                data.route_id,
                data.route_name,
                data.user_id,
                data.first_name+" "+data.last_name,
                data.tags,
                data.added,
                data.last_changed,
                data.geojson,
                data.visible,
                data.description,
                data.reviews,
                data.route_length.toPrecision(3)
            );
            console.log(this.route);
        },
        (error) => console.log(error)
    );
  }
}

路由對象只是存儲路由信息的對象(一些構造函數,解析輸入數據的方法等)。

在模板中,我使用像<h4>{{this.route.routeName}}</h4>這樣的路由對象。 當我嘗試運行此代碼時,出現許多錯誤,告訴我我正在嘗試訪問未定義的屬性。

我認為這是因為訂閱是異步事件,而 html 是在 NgOnInit 之后立即呈現的。 但是我不知道如何處理這個。 我想在從 API 加載模板之前向模板添加一些加載,然后向用戶顯示該數據。

誰能告訴我該怎么做?

您不需要在模板代碼中使用this.routeroute就足夠了。

您可以使用 Elvis 運算符來確保您不會遇到未定義的變量問題

<h4>{{route?.routeName}}</h4>

或者您可以將所有模板代碼包裝在 *ngIf 中

<div *ngIf="route!=null">
    <h4>{{route.routeName}}</h4>   
</div>

處理此問題的一種方法是,您可以像這樣定義模型來處理接收到的數據。

class Route {
    //Type you can set here
    route_id: string ;
    route_name: string ;
    user_id: string ;
    first_name: string;
    last_name: string; 
    tags: string ;
    added: string ;
    last_changed: string ;
    geojson: string ;
    visible: string ;
    description: string ;
    reviews: string ;
    route_length: number ;

    constructor(obj?: any) {
        this.route_id =  (obj && obj.route_id) || '';
        this.route_name =  (obj && obj.route_name) || '';
        this.user_id =  (obj && obj.user_id) || '';
        this.first_name =  (obj && obj.first_name) || '';
        this.last_name =  (obj && obj.last_name) || '' 
        this.tags =  (obj && obj.tags) || '';
        this.added =  (obj && obj.added) || '';
        this.last_changed =  (obj && obj.last_changed) || '';
        this.geojson =  (obj && obj.geojson) || '';
        this.visible =  (obj && obj.visible) || '';
        this.description =  (obj && obj.description) || '';
        this.reviews =  (obj && obj.reviews) || '';
        this.route_length =  (obj && obj.route_length && obj.route_length.toPrecision) || 0;
    }
}
// With this type of model you don't have to worry about API data.
// It will handle everything and the component 
// won't break even if you are accessing any nested object.

export class RouteDetailComponent implements OnInit{
public routeId:number;
public route:Route = new Route(); //Here you need to initialize your route
public actUrl: string;
    ngOnInit() {
    this.routeId = this.activateDroute.snapshot.params['id'];

    let data;
    this.httpService.getRoute(this.routeId)
        .subscribe(
            (response: Response) => {
                data = response.json().data;

                //Here you can simply pass the response from API 
                //and you will get the data in proper format
                this.route = new Route(data); 
                console.log(this.route);
            },
            (error) => console.log(error)
        );
    }
}

因此,即使 API 數據尚未到達,它也會被處理。

暫無
暫無

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

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