鍍金池/ 問(wèn)答/HTML5  HTML/ angular 編程時(shí)遇到的問(wèn)題:為何html模板中{{stock.id}} u

angular 編程時(shí)遇到的問(wèn)題:為何html模板中{{stock.id}} undefined

以下是html模板對(duì)應(yīng)的ts代碼
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Params, Router} from "@angular/router";

@Component({
selector: 'app-others',
templateUrl: './others.component.html',
styleUrls: ['./others.component.css']
})
export class OthersComponent implements OnInit {

// private info: number;
private stock: Stock;
private isPro: boolean;
private focus: boolean = false;

constructor(private routeinfo: ActivatedRoute) {

}

ngOnInit() {

//routeinfo.snapshot是快照方法接收參數(shù),只能在初始化的時(shí)候?qū)崿F(xiàn)一次
// this.info = this.routeinfo.snapshot.params["id"];
//routeinfo.params.subscribe訂閱方法是根據(jù)參數(shù)變化而進(jìn)行參數(shù)傳遞
this.routeinfo.params.subscribe((params: Params) => this.stock.id = params["id"]);
this.isPro = this.routeinfo.snapshot.data[0]["isPro"];

}

isForce() {

return this.focus;

}
}

export class Stock {
constructor(public id: number, public name: string) {

}
}

回答
編輯回答
笑忘初

模板中,在使用{{stock.id}}的標(biāo)簽或父級(jí)標(biāo)簽加stock對(duì)象空值判斷 *ngIf="stock" 即可

2017年3月29日 18:32