【问题标题】:add event listener if does not exist in angular 5 typescript如果 Angular 5 打字稿中不存在,则添加事件侦听器
【发布时间】:2018-11-16 19:26:29
【问题描述】:

我正在开发一个 Angular 混合应用程序,其中部分表单在 Angular 6 中,其余部分在 AngularJS 中。此应用程序是一个单页应用程序,内容跨十个选项卡。我已经用 Angular 6 替换了一个选项卡内容。我通过添加如下事件侦听器实现了自定义验证。

 private continueBtn : any;
 private continueFunction : any;

this.continueBtn = document.querySelector('.continue'); 

constructor{
this.continueFunction = (e: Event) => {   
//do custom validation
}
this.continueBtn.addEventListener('click', this.continueFunction);
}

作为自定义验证的一部分,我已删除事件侦听器。现在,当用户前进并返回时,由于没有侦听器,因此用户无法前进。 所以我想检查条件 --if(event listeners does not exist){ add event listener }

【问题讨论】:

  • 当你有最新的 Angular 6 时,为什么你必须使用 AngularJS?

标签: angular typescript


【解决方案1】:

我的意见是不要检查听众。而是将它添加到新的 oninit 并在 ondestroy 上销毁它。这将是安全且高效的。

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2010-11-26
    相关资源
    最近更新 更多