我正在尝试在Angular2应用程序中使用Select2。当我选择选项时,我可以打印出选择,但是有问题将所选选项绑定到我的角形式控件。
这是我的html:
<select id="fields" class="form-control fields-select" multiple="multiple"
formControlName="branchCode">
<option *ngFor="let field of _availableFields" [value]="field">{{field}}
</option>
</select>
这是我的TS文件:
private _availableFields: Array<any> = ['Value1', 'Value2', 'Value3'];
private _selectedFields: Array<string> = [];
export class AppComponent{
editForm : new FormGroup({
email: new FormControl('', Validators.required),
branchCode: new FormControl('', Validators.required),
});
}
ngAfterViewInit(){
jQuery('.fields-select').select2();
jQuery('.fields-select').on('change',
(e) =>
this._selectedFields = jQuery(e.target).val();
console.log(this._selectedFields);
);
};
我在var this._selectedfields中获得了选择。如何将我的选定值绑定到我的formcontrol" branch Code"中?
我认为与Angular一起使用jQuery select2
库是一个不错的主意,您应该检查NG2-SELECT,它是Angular的Select2库,您不会遇到这些问题。