Conditionally apply click event in Angular 4

后端 未结 6 1010
执笔经年
执笔经年 2020-12-03 16:56

Is it possible to define a condition in the template based on which a click handler is attached?

For instance, the closest I can get is evaluating a condition at the

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 17:21

    I would suggest you write a handler that performs the conditional action, this is the simplest way IMHO :

    In Component template :

    
    

    in Component code .ts :

    myClickHandler(event): void {
      if (this.isOverflown) {
        this.menu.toggle(event);
      }
    }
    

    EDIT after comment : if you really want to avoid binding (I don't understand why, but anyway) you can have a conditional component using *ngIf:

    
    
    

提交回复
热议问题