识别相同分量角度2

时间:2017-05-05 07:49:44

标签: angular typescript

我需要识别输入框值的变化,并在旧值与新值不同时触发事件。以下是我的模板和组件代码:

模板:

<input [(ngModel)]="filters[i]" type="text" size="30" pInputText (keyup)="filterByField($event, col.field, fieldType.TEXT, 'keyup')"
                                                        (focusout)="filterByField($event, col.field, fieldType.TEXT, 'focusout')"
                                                        class="{{'input-'+col.field}}" />

组件:

filterByField(event, field, fieldType, eventType) {
        if (fieldType === this.fieldType.DD) {
            event.originalEvent.stopPropagation();
            this.resetFilterBy(event.value.trim(), field);
            this.loadData(null, true);
        }
        else if (fieldType === this.fieldType.TEXT) {
            if (event.keyCode == 69 && field == this.fields.TASKID.field) {
                this.filters[3] = '';
            }
            if (event.keyCode === 13) {
                let filterValue = event.target.value;
                this.resetFilterBy(filterValue.trim(), field);
                this.loadData(null, true);
            }
        }
    }

有人可以建议,我如何检测输入值的变化

2 个答案:

答案 0 :(得分:1)

我会使用angular forms / FormControl并订阅它以获得更改。在distinctUntilChanged()之前添加subscribe应该会使您只获得新值

<强>组件

import { Component, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'component',
  templateUrl: './component.component.html',
  styleUrls: ['./component.component.scss']
})
export class Component implements OnInit {

  testForm = new FormControl('', Validators.required);

  ngOnInit() {
    this.testForm.valueChanges.distinctUntilChanged().subscribe(value => {
      // do something with the value
    })
  }
}

<强>模板

<input [formControl]="testForm" type="text">

答案 1 :(得分:-1)

你可以这样做:

Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image();
System.Windows.Media.Imaging.BitmapFrame frame = System.Windows.Media.Imaging.BitmapFrame.Create(System.Windows.Media.Imaging.BitmapSource.Create(
                        (int)pr.pixelBuffer.width,
                        (int)pr.pixelBuffer.height,
                        96,
                        96,
                        System.Windows.Media.PixelFormats.Rgb24,
                        null,
                        pr.pixelBuffer.buffer,
                        (int)(pr.pixelBuffer.stride * pr.pixelBuffer.height),
                        (int)pr.pixelBuffer.stride));
Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(frame.BaseUri);
image.Source = bitmapImage;//frame;
stackPanel.Children.Add(image)

注意:prevValue是组件中的实例变量,它保存输入字段的先前值,fireEventMethod()是执行您要执行的任何事件的方法。我认为你的意思是旧价值与新价值不同。