简体   繁体   English

Ionic2:ngOnChanges不触发

[英]Ionic2: ngOnChanges not triggering

I want to perform some action whenever the input value or the slider is changed, but why is ngOnchanges not getting triggered when text or range input is changed? 每当输入值或滑块更改时,我都想执行一些操作,但是为什么在更改文本或范围输入时不触发ngOnchanges?

Controller: 控制器:

 export class APage implements OnChanges {

      @Input() total: string;
      @Input() percentage: string;

      constructor() {

      }

      ngOnChanges(changes: {[propName: string]: SimpleChange}) {
          console.log('ngOnChanges');
      }

    }

Template: 模板:

 <ion-list>
    <ion-item>      
        <ion-label floating>Total</ion-label>
        <ion-input type="text" [(ngmodel)]="total"></ion-input>
    </ion-item>

    <ion-item>
        <ion-label stacked>Percentage: {{percentage}} %</ion-label>
        <ion-range [(ngModel)]="percentage" min="0" max="100">
        </ion-range>
    </ion-item>
</ion-list>

Update1: 更新1:

Add @Input decorater as per the suggestion which didn't help. 根据没有帮助的建议添加@Input decorater。

Update2: UPDATE2:

This works: 这有效:

ion-range [(ngModel)]="percentage" min="0" max="100" (ionChange)="inputChanged()">

But it doesn't work for ion-input: 但是它不适用于离子输入:

<ion-input type="text" [(ngmodel)]="total" (ionChange)="inputChanged()"></ion-input>

Update 3: 更新3:

It worked with keyup: 它与keyup一起工作:

<ion-input type="text" [(ngmodel)]="total" (keyup)="inputChanged()"></ion-input>

You might need to decorate aData with @Input() 您可能需要使用@Input()装饰aData

Edit: 编辑:

It's probably that you need to hook into the ionic events via the template. 您可能需要通过模板来了解离子事件。 If it's beta 8, the event is called (ionChange) , pre beta8 it's (change) , according to the changelog 根据更改日志 ,如果它是beta 8,则该事件称为(ionChange) ,而beta8之前是(change)

so something like <ion-range [(ngModel)]="percentage" (ionChange)="changeFunc()" min="0" max="100"> 所以类似<ion-range [(ngModel)]="percentage" (ionChange)="changeFunc()" min="0" max="100">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM