我有一个下拉列表的[(ngModel)]
和(ngModelChange)
属性,但如果我想使用[value]
属性加载任何默认值,我的ngModel不会给出当前更改的值。
我需要在下拉列表上进行双向绑定。一般来说,只有一个选择下拉列表它的工作,当它在我的jQuery表单中时出现问题。有人可以帮忙吗? My.Html:
<select class="form-control" name="state" [(ngModel)]="**myClient.address && myClient.address.state**" (ngModelChange)="getCitiesByState($event)" >
<option class="form-control" *ngFor="let state of states" [ngValue]='state'>{{state.name}}
</option>
</select>
答案 0 :(得分:1)
这是绑定到ngModel的下拉列表,默认值设置为“Second”/“2”:
import { Component } from '@angular/core';
@Component({
selector: 'app-native-element-playground',
template: `
<select name="option" [(ngModel)]="option">
<option *ngFor="let option of options" [ngValue]="option">
{{ option.name }}
</option>
</select>
{{option | json}}
`,
styleUrls: ['./native-element-playground.component.css']
})
export class NativeElementPlaygroundComponent {
options = [
{ name: 'First', code: '1' },
{ name: 'Second', code: '2' },
{ name: 'Third', code: '3' }
];
option = this.options[1];
}