简体   繁体   English

Angular 2选择不更改ngModel

[英]Angular 2 select not change ngModel

There are two select, the second depends on the first: 有两个选择,第二个取决于第一个:

<div class="form-group">
        <label class="col-xs-12 col-sm-2 control-label">Тип ТС:</label>
        <div class="col-xs-12 col-sm-10">
            <select class="form-control " type="text" [(ngModel)]="item.TransportTypeId">
                <option *ngFor='let type of transportTypes' [ngValue]='type.Id' [textContent]='type.Name'></option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-12 col-sm-2 control-label">Подтип ТС:</label>
        <div class="col-xs-12 col-sm-10">
            <select class="form-control " type="text" [(ngModel)]="item.TransportSubTypeId" (ngModelChange)="show()">
                <option [ngValue]="undefined">Не указано</option>
                <option *ngFor="let subType of transportSubTypes | filterBy: ['TransportTypeId']: item.TransportTypeId" [ngValue]='subType.Id' [textContent]='subType.Name'></option>
            </select>
        </div>
    </div>

It is assumed that the second select can be empty (undefined). 假定第二select可以为空(未定义)。 At the initial time all works fine, both select have values, when the second ngModelChange changes, it works. 最初,所有方法都可以正常工作,两个select都具有值,第二个ngModelChange更改时,它可以工作。 But if I select any value in the first select , when the filter for the second select returns an empty array, then visually the second select is dropped to the value Not chosen ( undefined ), but the ngModel does not equal undefined . 但是,如果我在第一个select选择了任何值,则当第二个select的过滤器返回一个空数组时,则第二个select在视觉ngModel被丢弃为值Not chosen ngModelundefined ),但是ngModel不等于undefined

For example: 例如:

item.TransportTypeId = 1;
item.TransportSubTypeId = 1;

change to: 改成:

item.TransportTypeId = 2;

The filterBy filter returns [], item.TransportSubType === 1 , but the value Not chosen is selected and the ngModelChange event does not occur. filterBy过滤器返回[], item.TransportSubType === 1 ,但是Not chosen了“ Not chosen ”值,并且不会发生ngModelChange事件。

Help me figure out how to make it so that select automatically resets the value of the model. 帮助我弄清楚如何制作它,以便select自动重置模型的值。

You need to add component logic as well to understand the query in right direction. 您还需要添加组件逻辑以正确理解查询。 But by looking to your query change the code as per below 但是通过查找查询来更改代码,如下所示

(ngModelChange)="show($event)" 
//component
show(event) { this.val = event.value; }

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

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