Required to dynamically call to Change() event on page load in Angular 8.3.21 and ng-select - Stack Overflow

admin2025-04-18  3

I'm unable to call the change() event when the page gets loaded.

I would like to display 'NONE' as a default text in ng-select drop-down untill api data gets loaded; thus, the 'NONE' value will be sent in request object while submitting its form. Anyhow, due to change event not getting fired on page load, I'm unable to proceed further.

\<ng-select \[items\]="apidata | async" bindValue="name" bindLabel="name" #refOption
(change)="onChangeMethod()"\>
\</ng-select\>

Technical specifications of <ng-select> control

  • ng-select : "3.7.2"
  • @angular/cli : "~8.3.21"

Note

  1. I had tried with adding Virwchild to call respective drop-down value but no luck.
  2. Tried code inside ngAfterViewInit()

I'm unable to call the change() event when the page gets loaded.

I would like to display 'NONE' as a default text in ng-select drop-down untill api data gets loaded; thus, the 'NONE' value will be sent in request object while submitting its form. Anyhow, due to change event not getting fired on page load, I'm unable to proceed further.

\<ng-select \[items\]="apidata | async" bindValue="name" bindLabel="name" #refOption
(change)="onChangeMethod()"\>
\</ng-select\>

Technical specifications of <ng-select> control

  • ng-select : "3.7.2"
  • @angular/cli : "~8.3.21"

Note

  1. I had tried with adding Virwchild to call respective drop-down value but no luck.
  2. Tried code inside ngAfterViewInit()
Share Improve this question asked Jan 29 at 15:47 GM_9GM_9 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Call the method directly on ngOnInit.

ngOnInit() {
  this.onChangeMethod(true)
}

onChangeMethod(onLoad =false) {
  let param = '';
  if (onLoad) {
    param = 'none';
  }
  this.http.get(`/api?tedst=${param}`).subscribe()
}
转载请注明原文地址:http://anycun.com/QandA/1744955021a89983.html