我想在角度6中初始化选择下拉列表的值,我在html视图中有以下代码
<select required
name="selectedQueryType"
[(ngModel)]="selectedQueryType"
#selectedQueryType="ngModel"
(ngModelChange)="queryTypeChange($event)"
(initialize)="selectedQueryType=queryTypes[0]"
class="form-control">
<option value="" disabled>QueryType</option>
<option *ngFor="let qtype of queryTypes"
[ngValue]="qtype"
[selected] = "item.qtype==qtype"
>
{{qtype}}
</option>
</select>
和上面的打字稿附带代码是
@Output() initialize: EventEmitter<any> = new EventEmitter();
public queryTypes = ["in","not_in"];
public selectedQueryType = {};
ngOnInit() {
this.initialize.emit();
}
我想在安装组件时运行代码(initialize)=“ selectedQueryType = queryTypes [0]”,以便选择select下拉列表的默认值,但它不起作用。请在此处提供帮助。
答案 0 :(得分:1)
用组件代码完成
["mammal" => ["dog", "cat"] , "reptile" => ["snake"]]
为什么@Output() initialize: EventEmitter<any> = new EventEmitter();
public queryTypes = ["in","not_in"];
public selectedQueryType = {};
ngOnInit() {
this.selectedQueryType=this.queryTypes[0];
this.initialize.emit();
}
是一个字符串,而queryTypes
是一个空对象(例如,不是空字符串)对我来说是个困惑。