Angular CLI从api

时间:2018-03-11 12:07:56

标签: javascript angular typescript angular-cli

我有一个服务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)
    );
  }
}

路径对象只是存储路径信息的对象(一些构造函数,解析输入数据的方法等)。

在模板I中使用路由对象,如<h4>{{this.route.routeName}}</h4>。 当我尝试运行此代码时,我收到很多错误,告诉我我正在尝试访问未定义的属性。

我认为这是因为订阅是异步事件,并且在NgOnInit之后立即呈现html。 但是我不知道如何处理这个问题。 我想在从API加载之前向模板添加一些加载,然后向用户显示该数据。

有谁能告诉我怎么做,拜托?

2 个答案:

答案 0 :(得分:1)

您无需在模板代码中使用this.routeroute即可。

您可以使用Elvis运算符确保不会遇到未定义的变量问题

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

或者您可以将所有模板代码包装在* ngIf

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

答案 1 :(得分:0)

您可以处理此问题的一种方法是,您可以定义这样的模型来处理收到的数据。

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数据尚未到达,也会处理它。