我在自己的Ionic 2項目中,使用卡片列出數(shù)據(jù):
<ion-card *ngFor="let item of inspects">
卡片中有一個導(dǎo)航按鈕,根據(jù)每項的數(shù)據(jù)生成連接打開百度地圖,我是這樣綁定的:
頁面:
<a [href]="nav(item)" target="_blank" ion-button icon-left clear small>
<ion-icon name="pin"></ion-icon>
<div>導(dǎo)航</div>
</a>
代碼:
nav(item) {
let url = `bdapp://map/navi?location=${item.lng},${item.lat}`;
if (Device.platform == 'iOS') {
url = `baidumap://map/direction?origin=34.264642646862,108.95108518068&destination=${item.lng},${item.lat}&mode=driving&coord_type=wgs84&src=webapp.navi.yourCompanyName.yourAppName`;
}
console.log(url);
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
我查看console,發(fā)現(xiàn)一直在輸出:
console一直在輸出
原來這是Angular2在change detection cycle中不停的調(diào)用綁定的方法nav(item)。因此,建議不要在屬性上綁定方法,因為調(diào)用太頻繁了,最好預(yù)先計算好然后綁定一個值就好。