Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda