简体   繁体   English

(change) vs (ngModelChange) 角度

[英](change) vs (ngModelChange) in angular

Angular 1 does not accept onchange() event, it's only accepts ng-change() event. Angular 1 不接受onchange()事件,它只接受ng-change()事件。

Angular 2, on the other hand, accepts both (change) and (ngModelChange) events, which both seems to be doing the same thing.另一方面,Angular 2 接受(change)(ngModelChange)事件,这两者似乎都在做同样的事情。

What's the difference?有什么不同?

which one is best for performance?哪一种最适合性能?

ngModelChange : ngModelChange

<input type="text" pInputText class="ui-widget ui-text"
    (ngModelChange)="clearFilter()" placeholder="Find"/>

vs change : VS变化

<input type="text" pInputText class="ui-widget ui-text" 
    (change)="clearFilter()" placeholder="Find"/>

(change) event bound to classical input change event. (change)事件绑定到经典输入更改事件。

https://developer.mozilla.org/en-US/docs/Web/Events/change https://developer.mozilla.org/en-US/docs/Web/Events/change

You can use (change) event even if you don't have a model at your input as即使您的输入中没有模型,您也可以使用 (change) 事件作为

<input (change)="somethingChanged()">

(ngModelChange) is the @Output of ngModel directive. (ngModelChange)@Output ngModel指令。 It fires when the model changes.当模型改变时它会触发。 You cannot use this event without ngModel directive.如果没有 ngModel 指令,您将无法使用此事件。

https://github.com/angular/angular/blob/master/packages/forms/src/directives/ng_model.ts#L124 https://github.com/angular/angular/blob/master/packages/forms/src/directives/ng_model.ts#L124

As you discover more in the source code, (ngModelChange) emits the new value.当您在源代码中发现更多信息时, (ngModelChange)会发出新值。

https://github.com/angular/angular/blob/master/packages/forms/src/directives/ng_model.ts#L169 https://github.com/angular/angular/blob/master/packages/forms/src/directives/ng_model.ts#L169

So it means you have ability of such usage:所以这意味着你有这样的使用能力:

<input (ngModelChange)="modelChanged($event)">
modelChanged(newObj) {
    // do something with new value
}

Basically, it seems like there is no big difference between two, but ngModel events gains the power when you use [ngValue] .基本上,两者之间似乎没有太大区别,但是当您使用[ngValue]时, ngModel事件会获得强大的力量。

  <select [(ngModel)]="data" (ngModelChange)="dataChanged($event)" name="data">
      <option *ngFor="let currentData of allData" [ngValue]="currentData">
          {{data.name}}
      </option>
  </select>
dataChanged(newObj) {
    // here comes the object as parameter
}

assume you try the same thing without " ngModel things"假设你在没有“ ngModel东西”的情况下尝试同样的事情

<select (change)="changed($event)">
    <option *ngFor="let currentData of allData" [value]="currentData.id">
        {{data.name}}
    </option>
</select>
changed(e){
    // event comes as parameter, you'll have to find selectedData manually
    // by using e.target.data
}

在 Angular 7 中, (ngModelChange)="eventHandler()"绑定到[(ngModel)]="value"值更改之前触发(change)="eventHandler()"绑定到的值之后触发[(ngModel)]="value"已更改。

As I have found and wrote in another topic - this applies to angular < 7 (not sure how it is in 7+)正如我在另一个主题中找到并写的那样 - 这适用于 angular < 7(不确定它在 7+ 中的情况)

Just for the future只为未来

we need to observe that [(ngModel)]="hero.name" is just a short-cut that can be de-sugared to: [ngModel]="hero.name" (ngModelChange)="hero.name = $event" .我们需要观察到[(ngModel)]="hero.name"只是一个可以去[ngModel]="hero.name" (ngModelChange)="hero.name = $event"[ngModel]="hero.name" (ngModelChange)="hero.name = $event"

So if we de-sugar code we would end up with:因此,如果我们对代码进行脱糖,我们最终会得到:

<select (ngModelChange)="onModelChange()" [ngModel]="hero.name" (ngModelChange)="hero.name = $event">

or

<[ngModel]="hero.name" (ngModelChange)="hero.name = $event" select (ngModelChange)="onModelChange()">

If you inspect the above code you will notice that we end up with 2 ngModelChange events and those need to be executed in some order.如果您检查上面的代码,您会注意到我们最终有 2 个ngModelChange事件,这些事件需要按某种顺序执行。

Summing up: If you place ngModelChange before ngModel , you get the $event as the new value, but your model object still holds previous value.总结:如果你把ngModelChange之前ngModel ,您将获得$event作为新的价值,但你的模型对象仍然保持以前的值。 If you place it after ngModel , the model will already have the new value.如果你把它ngModel之后,模型就会有新的值。

SOURCE来源

1 - (change) is bound to the HTML onchange event. 1 - (change)绑定到 HTML onchange 事件。 The documentation about HTML onchange says the following :关于 HTML onchange 的文档说明如下:

Execute a JavaScript when a user changes the selected option of a <select> element当用户更改<select>元素的选定选项时执行 JavaScript

Source : https://www.w3schools.com/jsref/event_onchange.asp来源: https : //www.w3schools.com/jsref/event_onchange.asp

2 - As stated before, (ngModelChange) is bound to the model variable binded to your input. 2 -如前所述, (ngModelChange)绑定到绑定到您输入的模型变量。

So, my interpretation is :所以,我的解释是:

  • (change) triggers when the user changes the input (change)用户改变输入时触发
  • (ngModelChange) triggers when the model changes, whether it's consecutive to a user action or not (ngModelChange)在模型更改时触发,无论是否与用户操作连续

As per my experience (change) and (ngModelChange) has two different usage.根据我的经验(change)(ngModelChange)有两种不同的用法。

  1. (ngModelChange) triggers when HTML renders, user changed the value of that element. (ngModelChange)在 HTML 呈现时触发,用户更改了该元素的值。

  2. (change) triggers when user changes the value and leave the element focus. (change)当用户更改值并离开元素焦点时触发。

Usage:用法:

  1. (ngModelChange) : when you have critical things that depends on html any type of changes that you have to handle. (ngModelChange) :当您有关键的事情依赖于 html 时,您必须处理任何类型的更改。
  2. (change) : When you have to handle only value changes done by user. (change) :当您只需要处理用户所做的值更改时。

Note: Be careful while using the (ngModelChange) because sometimes it will give you maximum call stack issue and your form will stuck.注意:使用(ngModelChange)要小心,因为有时它会给您带来最大的调用堆栈问题,并且您的表单会卡住。

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

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