HTML:
<select data-placeholder="Skill List" style="width:100%;" class="chzn-select form-control" multiple="multiple">
<option *ngFor="#skill of allSkills" [ngValue]="skill">{{skill}}
</option>
</select>
TS:
allSkills = ['Welding', 'Forklifting'];
selectedSkill = ['Welding'];
ngOnInit(): void {
jQuery('.chzn-select').select2();
}
如何最初添加 selectedSkill ?
答案 0 :(得分:1)
只需将ngModel
绑定添加到select
标记:
<select data-placeholder="Skill List"
[(ngModel)]="selectedSkill"
style="width:100%;"
class="chzn-select form-control"
multiple="multiple">
<option *ngFor="#skill of allSkills" [ngValue]="skill">
{{skill}}
</option>
</select>
在您的代码中:
allSkills = ['Welding', 'Forklifting'];
selectedSkill = allSkills[0]; // or just 'Welding'
ngOnInit(): void {
jQuery('.chzn-select').select2();
}