@Output EventEmitter的角度六设置初始值 [英] Angular six set initial value for @Output EventEmitter

查看:210
本文介绍了@Output EventEmitter的角度六设置初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组件

     <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>

其中有一个@OutPut

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

我在我的应用中使用了此

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

哪个将组件中的代码调用为 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.

是否可以通过@Output获取默认值? ?
或您将如何实现?

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

推荐答案

只需在ngOnInit中发出初始值

Simply emit the initial value in ngOnInit

export class YourClass {

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

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

这篇关于@Output EventEmitter的角度六设置初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆