Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular six set initial value for @Output EventEmitter

I have a component

     <p style="padding: 5px">

      <select [(ngModel)]='thisDD' name="nameDD" id="idDD" (ngModelChange)="updateDD(thisDD)" class="form-control">
        <option *ngFor="let thing of thingies" [value]="thing.thingID">{{thing.ThingName}} ({{thing.ThingCode}})</option>
      </select>  

     </p>

Which has an @OutPut

 @Output() selectedValue = new EventEmitter<object>();

And I use this in my app

<my-dropdown (selectedValue)="setValue($event)"></my-dropdown> 

Which calls code in the component to "setValue"

setValue(event){
this.currValue=event;
}

This all works great when the value of the drop down is changed but I have other components that rely on a value being set when the application is loaded.

Is there a way to get the value I defaulted my component to through @Output? or how would you accomplish this?

like image 779
Funn_Bobby Avatar asked Aug 29 '26 23:08

Funn_Bobby


1 Answers

Simply emit the initial value in ngOnInit

export class YourClass {

    @Output() selectedValue = new EventEmitter<object>();

    ngOnInit() {
        this.selectedValue.emit({{your initial value}});
    }
}
like image 76
Josh Harkema Avatar answered Sep 02 '26 13:09

Josh Harkema



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!