Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

2K
Vistas
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.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Santiago Trujillo Denunciar

0

For Reactive forms, you can bind the value while creating `FormGroup`.

In TS:

 import { FormBuilder, Validators, FormGroup} from '@angular/forms';

 accountForm: FormGroup; // declaring form group


constructor(private fb: FormBuilder) {} 

ngOnInit(): void {
    this.createForm();
}

createForm(): void {
    this.accountForm = this.fb.group({
       trial: ['Yes', [Validators.required]] // Its default value
    });
  }

In case, if the value is dynamic (eg. from API call), you can set it with this.accountForm.patchValue({trial: 'abcd'});

In HTML:

<form class="form"
    [formGroup]="accountForm"
    (ngSubmit)="onSubmit(accountForm)"
    autocomplete="off"
    novalidate>
    <div  class="form-row">
    <div class="col-sm-4" >
      <div class="form-group">
      <label>Trial</label>
      <ng-select
         [items]="booleanDropdown"
         [multiple]="false"
         [selectableGroup]="true"
         [closeOnSelect]="true"
         [clearable]="false"
         formControlName="trial">
      </ng-select>
      </div>
  </div>
</div>
about 4 years ago · Santiago Trujillo Denunciar

0

You have to either define a component type object for the variable or or you can get the first element of the list and set it as a defualt variable.

    @ViewChildren(SelectComponent) private selectComponents: QueryList<SelectComponent>;
defualtComponent: any; ngAfterViewInit() { this.defaultComponent = this.selectComponents.get(0); }

This will select the first element if that is what you want as ypur default element.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda