I have used angular2 drop-down for multiple value using follwing code:
<ng-select [options]="dropDownSKUNumber" #SKUNumberDrp multiple="true" placeholder="Select SKU Number" [allowClear]="true"></ng-select>
This code is working properly for me at the time of save multiple value. Then but in view mode, I want that values by default in selected mode in dropdown, But i can't be able to do this.
How to solve it? using above code.
答案 0 :(得分:5)
由于您使用的是选项,因此您必须使用this ng2-select。
在这种情况下,您应该将其绑定到ngModel
,然后在脚本的init上更新该值:
即。 HTML
<ng-select
[multiple]="true"
[options]="myOptions"
[(ngModel)]="mySelectValue">
</ng-select>
TS
export class App implements OnInit {
myOptions: Array<any>;
mySelectValue: Array<string>; // Array of strings for multi select, string for single select.
ngOnInit() {
this.myOptions = [
{value: 'a', label: 'Alpha'},
{value: 'b', label: 'Beta'},
{value: 'c', label: 'Gamma'},
];
this.mySelectValue = ['b', 'c'];
}
}
答案 1 :(得分:1)
根据angular2 select,您可以使用ngModel
设置初始值:
您可以使用options
作为数组,使用ngModle
作为组件中的选定值,如下所示:
//Component
public dropDownSKUNumber : any[] = [
{ value: 1, label : 1 },
{ value: 2, label : 2 },
{ value: 3, label : 3 }
];
public selectedValue: string[] = [ '2', '3' ];
//HTML
<ng-select [options]="dropDownSKUNumber" #SKUNumberDrp multiple="true" placeholder="Select SKU Number" [(ngModel)]="selectedValue" [allowClear]="true"></ng-select>