Angular six set initial value for @Output EventEmitter

心不动则不痛 提交于 2019-12-13 14:03:26

问题


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?


回答1:


Simply emit the initial value in ngOnInit

export class YourClass {

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

    ngOnInit() {
        this.selectedValue.emit({{your initial value}});
    }
}


来源:https://stackoverflow.com/questions/52598734/angular-six-set-initial-value-for-output-eventemitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!