Angular2:如何取消下拉更改事件?
如何取消Angular 2中的下拉更改事件 HTML:Angular2:如何取消下拉更改事件?,angular,Angular,如何取消Angular 2中的下拉更改事件 HTML: 您可以这样尝试,注意ngModel不是直接绑定的: HTML: 我想你需要这个@WannaStream jQuery在更改值后触发事件,Angular2在更改值前触发事件是否确定?更改是将函数绑定到onchange事件。我不认为Angular有不同的行为这很奇怪。。。请参阅上面的代码。oldStatusId和newStatusId与我预期的一样我明白你的意思。实际的数据绑定更新可能发生在onchange事件之后。你试过电子商务吗? <
您可以这样尝试,注意ngModel不是直接绑定的: HTML:
我想你需要这个@WannaStream jQuery在更改值后触发事件,Angular2在更改值前触发事件是否确定?更改是将函数绑定到onchange事件。我不认为Angular有不同的行为这很奇怪。。。请参阅上面的代码。oldStatusId和newStatusId与我预期的一样我明白你的意思。实际的数据绑定更新可能发生在onchange事件之后。你试过电子商务吗?
<select [(ngModel)]="statusId" (change)="onStatusChange($event)">
<option *ngFor="let status of statuses" [ngValue]="status.id">{{status.text}}</option>
</select>
onStatusChange(e: Event) {
var oldStatusId = this.statusId;
var newStatusId = e.target.value;
if(this.submitNewValue(newStatusId)){
console.log('value changed');
} else {
console.log('value not changed');
// How to cancel this and keep the previously selected value in the dropdown?
}
}
<select (change)="onChange($event, statusId)">
<option value="" class="dropdown-item">Select Status</option>
<option *ngFor="let status of statuses" [value]="status.id">{{status.text}}</option>
</select>
onChange(event, statusId) {
if (isValid(event.target.value)) {
statusId = event.target.value;
}
else{
statusId = "";
event.target.selectedIndex = "0";
}
}