我有自定义的select2指令。
<select select2 name="activityId" #activity="ngModel" class="form-control" [(ngModel)]="company.activityId" required>
<option [value]=""></option>
<option *ngFor="let item of lookup.activities;" [value]="item.id">{{item.text}}</option>
</select>
在select2中选择一个活动值后,进入具有局部引用变量“ #activity”的选择标签。无效未从true更新为false。
如何从指令代码中自定义更新它?
谢谢
export class Select2Directive {
constructor(private el: ElementRef, private model: NgModel) {
$(this.el.nativeElement).select2({
width: '100%',
theme: "bootstrap",
placeholder: "SELECT",
allowClear: true
});
var model = this.model;
$(this.el.nativeElement).on('select2:select select2:unselecting', function (e: any) {
let value = (e.type == 'select2:unselecting') ? "" : e.currentTarget.value;
model.viewToModelUpdate(value);
});
}
}
答案 0 :(得分:0)
我找到了答案
代替model.viewToModelUpdate(value)需要编写model.reset(value)