简体   繁体   English

如果不在ngModel指令中提供模型名称,ngModelChange如何工作

[英]How does ngModelChange works without providing model name in ngModel directive

How does ngModelChange() works? ngModelChange()如何工作?

(ngModelChange) is the @Output of ngModel directive. (ngModelChange)是ngModel指令的ngModel It fires when the model changes. 它在模型改变时触发。 You cannot use this event without ngModel directive 如果没有ngModel指令,则不能使用此事件

but I am not sure, how does( ngModelChange ) it works, if I am use ngModelChange() event, even i am not providing model name to ngModel . 但我不确定,它是如何工作的( ngModelChange ),如果我使用ngModelChange()事件,即使我没有ngModel提供模型名称

<input #gb type="text" pInputText class="ui-widget ui-text" **ngModel**  
 (ngModelChange)="functionName($event)">

Yes, ngModelChange() work without providing model name to ngModel. 是的,ngModelChange()在不向ngModel提供模型名称的情况下工作。

cause of this happen, (ngModelChange) is the @Output of ngModel directive. 发生这种情况的原因,(ngModelChange)是ngModel指令的@Output。 when insert some value in input that time emitEvent is become true which is by default false (so it not fire page load on initial time). 当在输入中插入一些值时, emitEvent变为true ,默认为false(因此它不会在初始时激活页面加载)。

_this.updateValueAndValidity({ emitEvent: false }); _this.updateValueAndValidity({emitEvent:false});

you can find at \\@angular\\forms\\esm5\\forms.js ► line no 3850 你可以在\\ @angular \\ forms \\ esm5 \\ forms.js找到►行号3850

If emitEvent is true , this change will cause a valueChanges event on the FormControl to be emitted. 如果emitEventtrue ,则此更改将导致FormControl上的valueChanges事件被发出。 This defaults to true (as it falls through to updateValueAndValidity ). 默认为true(因为它落到updateValueAndValidity )。

If emitViewToModelChange is true , an ngModelChange event will be fired to update the model. 如果emitViewToModelChangetrue ,则将触发ngModelChange事件以更新模型。 This is the default behavior if emitViewToModelChange is not specified. 如果未指定emitViewToModelChange则这是默认行为。

If emitModelToViewChange is true , the view will be notified about the new value via an onChange event. 如果emitModelToViewChangetrue ,则将通过onChange事件通知视图有关新值的信息。

now question is that why get same value in $event which is inserted in input instead of ture , that cause 现在的问题是,为什么在$ event中得到相同的值,这个值插入输入而不是ture ,这就是原因

FormControl.prototype.setValue = /** FormControl.prototype.setValue = / **

function (value, options) {
        var _this = this;
        if (options === void 0) { options = {}; }
        (/** @type {?} */ (this)).value = this._pendingValue = value;
        if (this._onChange.length && options.emitModelToViewChange !== false) {
            this._onChange.forEach(function (changeFn) { return changeFn(_this.value, options.emitViewToModelChange !== false); });
        }
        this.updateValueAndValidity(options);
    };

same file line no 3911 to 3919 相同的文件行号3911至3919

In the Source code ngModelChange is just an event emitter. 源代码中, ngModelChange只是一个事件发射器。

  @Output('ngModelChange') update = new EventEmitter();

It fires when the viewToModelUpdate function is executed. 它在执行viewToModelUpdate函数时触发。

viewToModelUpdate(newValue: any): void {
  this.viewModel = newValue;
  this.update.emit(newValue);
}

ngModel can be anything and does not have a direct link to anything else. ngModel可以是任何东西,并且没有与其他任何东西的直接链接。 In the code it is declared and it is only used in a function called ngOnChanges 在声明的代码中,它仅用于名为ngOnChanges的函数中

@Input('ngModel') model: any;

ngOnChanges(changes: SimpleChanges) {
  this._checkForErrors();
  if (!this._registered) this._setUpControl();
  if ('isDisabled' in changes) {
    this._updateDisabled(changes);
  }

  if (isPropertyUpdated(changes, this.viewModel)) {
    this._updateValue(this.model);
    this.viewModel = this.model;
  }
}

I could be wrong here but it looks to me that ngModel is not the single source of truth but this.viewModel seems to be, because of this ngModel does not need a value for ngModelChange to work as it opporates seporatetly from the ngModel value. 我可能是错在这里,但在我看来那ngModel是不是真理的唯一来源,但this.viewModel似乎是,因为这个ngModel不需要值ngModelChange工作,因为它从seporatetly opporates ngModel值。


Hope this helps. 希望这可以帮助。

you try it without ngModel 你没有ngModel尝试它

<select (change)="changed($event)">
     <option *ngFor="let data of allData" [value]="data.id">
           {{data.name}}
       </option>
</select>

changed(e){
   //event comes as parameter and then find data manually
   //by using e.target.data

}

              OR BY ID

 <inputtype="text" #byid (change)="onChange(byid.value)" />
  onChange(title:string){
   alert(title);
   }

You can try by passing id into input 您可以尝试将id传递给输入

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

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