Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set default value using ng-select in angular2?

I have my code in HTML like this.

      <div class="col-md-10">
          <ng-select [items]="tests" (data)="updateSelected($event, 'test')">

          </ng-select>
       </div>

My typescript code is as follows

@Input() tests: Option[] = []; I have tests coming as input from previous component

     @ViewChildren(SelectComponent)
     private selectComponents: QueryList<SelectComponent>;
    
    ngAfterViewInit() {
            this.selectComponents;
        }

How do I set some value as default value here in selectComponents or is there any other way to set some value as default value using ng-select.

like image 282
code1 Avatar asked Mar 22 '17 19:03

code1


People also ask

How do I set default selected value in ng options?

Use ng-init to set default value for ng-options . Save this answer.

How do I set the default value in select?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.


1 Answers

Use ngModel binding as below.

<ng-select options="users" [(ngModel)]="selectedValue">
</select>
ngOnInit() {
  this.selectedValue = ["1"]
}

Array item value is option id. https://basvandenberg.github.io/ng-select may be helpful for you.

like image 149
avery Avatar answered Sep 30 '22 19:09

avery