我正在尝试将select2集成到我的html模板中,但是我一直收到此错误。我希望能够一次选择多个选项 我不确定如何去做。 帮助将不胜感激。
admin.ts
import { Component, OnInit } from '@angular/core';
import { Select2OptionData } from 'ng2-select2';
@Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss']
})
export class AdminComponent implements OnInit {
public candidateData: Array<Select2OptionData>;
public options: Select2Options;
public value: string[];
public current: string;
constructor() { }
ngOnInit() { this.candidateData = [
{ id: '1', text: 'Mohammadu Buhari' },
{ id: '2', text: 'Atiku Abubakr' },
{ id: '3', text: 'John Doe'},
{ id: '4', text: 'Someone Else'}];
this.options = {
multiple: true
};
this.current = this.value.join(' | ');
}
changed(data: {value: string[]}) {
this.current = data.value.join(' | ');
}
}
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
<label>Candidates</label>
<select2
[data] = "exampleData"
[options] = "options"
[width] = "500"
[value] = "value"
(valueChanged) = "changed($event)"
></select2>
</div>
</div>
</div>
答案 0 :(得分:0)
您必须初始化模型,因为当您尝试在undefined
中访问模型时,模型为ngOnInit
。
public value: string[] = [];