【问题标题】:ionic 3 : change button color dynamically : Data Binding is not workingionic 3:动态更改按钮颜色:数据绑定不起作用
【发布时间】:2018-03-26 23:27:43
【问题描述】:

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

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

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    您应该为此使用 Angular ngZone 服务。在这里阅读更多:Angular Zones

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

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 2015-08-13
      • 2019-09-24
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 2017-11-13
      相关资源
      最近更新 更多