简体   繁体   English

Angular2自动触发ngModelChange

[英]Angular2 trigger ngModelChange automatically

So I have the following code: 所以我有以下代码:

<div class="form-group">
<label for="backings_select">Backing Single</label>
<select class="form-control"
        required
        [(ngModel)]="selectedBacking" 
        name="backings_select"
        (ngModelChange)="storeValueRedux($event, count)">
  <option *ngFor="let backing of backings" [ngValue]="backing.id" [selected]="backings.length === 1">{{backing.name}}</option>
</select>

It populates a select box with results from a service call, if the array length is 1, it auto selects the only option available, this works fine. 它使用服务调用的结果填充选择框,如果数组长度为1,它会自动选择唯一可用的选项,这样效果很好。

However, by default the select box uses a value from the component as its default value. 但是,默认情况下,选择框使用组件中的值作为其默认值。

So when the service call is made, if the array only has a length of one, the value of the model is changing, but because its being auto selected (not by user input) the storeValueRedux event is not firing. 因此,在进行服务调用时,如果数组的长度仅为1,则模型的值将发生变化,但是由于自动选择了模型(不是通过用户输入), 因此不会触发storeValueRedux事件。

However, if the array has more than one entry, and then is selected by a user, the function is called and works as required. 但是,如果数组具有多个条目,然后由用户选择,则该函数将被调用并按要求工作。 Is there anyway to trigger ngModelChange in the instance that backings.length = 1? 无论如何,在backings.length = 1的实例中触发ngModelChange?

You can't use a condition inside your method calls in HTML but you can use change and handle the condition inside your method as below 您不能在HTML中的方法调用中使用条件,但可以使用change并在方法内部处理条件,如下所示

 <select class="form-control"
    required
    [(ngModel)]="selectedBacking" 
    name="backings_select"
    (change)="storeValueRedux($event, count)">
        <option *ngFor="let backing of backings" [ngValue]="backing.id"
            [selected]="backings.length === 1">{{backing.name}}</option>

 selectedBacking:any{};
  backings:any[]=[
    {id:1, name:'a'},
    {id:2, name:'a'}
    ]

  storeValueRedux(a,b){
     if(this.backings.length!=1){
    console.log(this.selectedBacking);
    console.log(a,b);
  }
 }

LIVEDEMO 现场演示

The service that returned my backings was an observable, so I modified the subscribe from: 可以得到支持的服务是可观察到的,因此我从以下方式修改了订阅:

.subscribe(
        results => {this.backings = results},
        error => console.log(error),        
);  

to: 至:

.subscribe(
        results => {this.backings = results, this.testBackingLength()},
        error => console.log(error),        
);  

And then added: 然后添加:

testBackingLength(){
    /* If the length of the arrau is only one, the template auto selects it
    and does not trigger ngOnChange, so we need to manually trigger it here,
    this function is called from the subscribe a few lines up */
    if (this.backings.length === 1) {
    this.storeValueRedux(this.backings[0]['id'], this.count)}
}

So each time my service is called, it tests the length of the array. 因此,每次调用我的服务时,它都会测试数组的长度。 If the length of the array is 1, it will auto call my function. 如果数组的长度为1,它将自动调用我的函数。

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

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