离子3:动态更改按钮颜色:数据绑定不起作用

时间:2018-03-26 15:11:54

标签: angular ionic-framework

我正在使用Ionic3而我正试图将动态颜色设置为按钮,我惊讶地发现数据绑定存在问题:

about.ts:

import { Component, ViewChild, ElementRef } from '@angular/core';

export class AboutPage {

  markers:any[]=Array();
  // many code between those two lines
  test(){
    alert(this.markers.length);
  }

}

home.html的:

<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会更新,并且实现会按预期工作。

1 个答案:

答案 0 :(得分:1)

您应该使用Angular ngZone服务。在此处阅读更多内容:Angular Zones

this.ngZone.run(() => {
     this.markers.push(marker);
});