我正在使用Ionic3而我正试图将动态颜色设置为按钮,我惊讶地发现数据绑定存在问题:
import { Component, ViewChild, ElementRef } from '@angular/core';
export class AboutPage {
markers:any[]=Array();
// many code between those two lines
test(){
alert(this.markers.length);
}
}
<ion-content>
<ion-fab >
<button ion-fab color="light" (click)="test()" mini [color]="markers.length > 0 ? 'primary' : 'danger'">
{{markers.length}}
</button>
</ion-fab>
</ion-content>
因此,当我按下标记时,按钮内容不会改变,但是当我点击按钮时,markers.length会更新,并且实现会按预期工作。
答案 0 :(得分:1)
您应该使用Angular ngZone服务。在此处阅读更多内容:Angular Zones
this.ngZone.run(() => {
this.markers.push(marker);
});