简体   繁体   English

旧值保留在 ngModel 更改上

[英]Old value kept on ngModel change

Dealing with a table which need a filter by data, I've created the following screen:处理需要按数据过滤的表,我创建了以下屏幕:

在此处输入图片说明

What I need is to remove values outside the date start criteria of the filter so I've done this on my EsightComponent :我需要的是删除过滤器的日期开始条件之外的值,所以我已经在我的EsightComponent上完成了这个:

startDateChange($event, dt, col) {
  this.loading = true;
  this.eSightEndSubscription = this.eSightService.getByContractId(this.contractId).subscribe(
    res => {
      const filterResults = this.reads.filter((read) => {
        const iteratorDate = new Date(20 + read.timestamp.slice(-2), read.timestamp.slice(0, 2) - 1, 1);
        console.log(this.startDateFilter);
        return iteratorDate <= this.startDateFilter;
      }, this);

      this.processRightAnswer(filterResults);
    },
    error => {
      console.log('ERROR');
    },
    () => {
      this.loading = false;
    });
}

And this is my view:这是我的观点:

<h4>Lecturas eSight <small>contrato {{ contractId }}</small></h4>

<p-calendar dateFormat="mm/yy" [(ngModel)]="startDateFilter" (onBlur)="startDateChange($event, dt, col)"></p-calendar>
<p-calendar [(ngModel)]="endDateFilter" (change)="startDateChange($event, dt, col)"></p-calendar>

<p-dataTable [value]="reads" [rows]="30" [paginator]="true" [loading]="loading" selectionMode="single" scrollable="true" #dt>
  <ng-container *ngFor="let col of cols; let i = index;">
    <p-column [field]="col.field" [header]="col.header" *ngIf="i === 0" [style]="{ 'width': '170px' }">
    </p-column>
    <p-column [field]="col.field" [header]="col.header" *ngIf="col.header.length <= 10" [style]="{ 'width': '55px' }"></p-column>
    <p-column [field]="col.field" [header]="col.header" *ngIf="col.header.length > 10" [style]="{ 'width': '170px' }"></p-column>
  </ng-container>
</p-dataTable>

<button class="btn btn-warning" (click)="onReturn()">Atrás</button>

Based on tho way data binding principle, this.startDateFilter should catch changes on it, but it doesn't.基于tho way数据绑定原则, this.startDateFilter应该捕捉它的变化,但它没有。 If I select a date on the calendar, it always displays the previous value, not the selected new one.如果我在日历上选择一个日期,它总是显示以前的值,而不是选择的新值。 That is seen on console.log(this.startDateFilter) line.这可以在console.log(this.startDateFilter)行上看到。

I've checked I could use ngOnChanges to catch changes and to have access to both old and new values, but this hook just works for @Input() values and this is not my case.我已经检查过我可以使用ngOnChanges来捕获更改并访问旧值和新值,但是这个钩子只适用于@Input()值,这不是我的情况。

I wish to retrieve the value selected by the user and not an old one.我希望检索用户选择的值而不是旧值。

Thanks.谢谢。

It's happens because ngModel triggers after change datection, but you are watching for blur, that fires before detection发生这种情况是因为 ngModel 在更改日期后触发,但您正在观察模糊,在检测之前触发

You should getting current value from $event, not from model value.您应该从 $event 中获取当前值,而不是从模型值中获取。 Something line东西线

$event.value

基于 DmitriyKhirniy 的回答(非常感谢!!!),最终有效的是:

$event.target.value

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

相关问题 更改ngModel值以切换选项卡 - Change ngModel value to switch tabs Angular指令将ngmodel值更改为undefined - Angular directive change ngmodel value to undefined AngularJS - 如何在custom指令中更改ngModel的值? - AngularJS - how to change the value of ngModel in custom directive? AngularJS - 是否可以在链接或编译中更改指令上的ngModel属性的值? - AngularJS - Is it possible to change the value of ngModel attribute on directive in link or compile? 观看ngModel值从父元素上的指令更改 - Watch ngModel value change from directive on parent element 用Javascript更改输入值后如何强制Angular 2 [(ngModel)]更改? - How to force Angular 2 [(ngModel)] change after changing input value with Javascript? Angular指令将ngmodel值从null更改为未定义的中断验证 - Angular directive change ngmodel value from null to undefined breaks validation 如何从 javascript/控制台更改 angular ngModel 值? - How can i change angular ngModel value from javascript/console? 改变价值JS,PHP获得旧价值 - Change value JS, PHP getting old value 当我以编程方式将新值推入该数组时,该数组的Angular5输入字段未显示ngModel的任何旧值 - Angular5 input fields of array does not show any old value of ngModel when I programmatically push a new value to that array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM