要将[(ngModel)]分配给带有Angular和Materialize CSS的'selected'选项的自定义多选,可以按照以下步骤进行操作:
<div class="input-field">
<select id="customSelect" multiple [(ngModel)]="selectedOptions">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<!-- 其他选项 -->
</select>
<label for="customSelect">Custom Select</label>
</div>
selectedOptions: string[] = [];
ngOnInit() {
this.selectedOptions = ['option1', 'option2']; // 设置初始选中的选项
}
ngAfterViewInit() {
const customSelect = document.getElementById('customSelect');
M.FormSelect.init(customSelect, {
// 可以根据需要进行其他配置
});
this.updateSelectedOptions();
}
updateSelectedOptions() {
const customSelect = document.getElementById('customSelect');
const instance = M.FormSelect.getInstance(customSelect);
instance.getSelectedValues().forEach(value => {
const option = document.querySelector(`option[value="${value}"]`);
option.setAttribute('selected', 'selected');
});
}
这样,就可以将[(ngModel)]分配给带有Angular和Materialize CSS的'selected'选项的自定义多选。请注意,这只是一种实现方式,具体的实现方式可能因项目的需求和使用的版本而有所不同。
领取专属 10元无门槛券
手把手带您无忧上云