【问题标题】:event emitter works one time but not a second time Angular 8事件发射器工作一次但不是第二次Angular 8
【发布时间】:2019-12-03 14:23:30
【问题描述】:

我有这个变量控制对话框是否显示,它在第一次工作,但在第二次不工作,发出执行但不再调用接收函数。

父类:

  isLogin :boolean ;

  constructor(...){
    this.isLogin = false;
}
  receiveNotification(notification: boolean): void {
    this.isLogin = notification;
  }

父 html:

<login-dialog   *ngIf="!isLogin"  name="{{name}}" (notify)="receiveNotification($event)"></login-dialog>

在子类中: 我有一个函数,当触发调用发出和发出确实被调用时,它不会第二次触发父级上的函数

@Output() notify = new EventEmitter<any>();

  exampleFunction(){
 this.notify.emit(true);
}

我认为这可能与 ngIf 相关,但不确定,这里有什么问题?

【问题讨论】:

    标签: angular typescript events dialog angular-event-emitter


    【解决方案1】:

    这可能是因为您发出一个真值,但在父模板上您正在检查 !true 这是假的。

    根据您当前的逻辑,您应该发出一个错误值或将父模板中的条件更改为*ngIf="isLogin"

    【讨论】:

      【解决方案2】:

      尝试使用 hidden 代替 ngIf,因为 ngIf 从 DOM 中移除组件但 hidden 只是隐藏它。

      <login-dialog   [hidden]="isLogin"  name="{{name}}"...></login-dialog>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-24
        • 2011-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-31
        • 1970-01-01
        相关资源
        最近更新 更多