简体   繁体   English

Angular2 EventEmitter和preventDefault()

[英]Angular2 EventEmitter and preventDefault()

Is there a way in Angular2 to somehow prevent the default for events using the EventEmitter? Angular2中有没有办法以某种方式阻止使用EventEmitter的事件的默认值?

I have the following scenario: 我有以下场景:

import {Component, Output, EventEmitter} from 'angular2/core'

@Component({
  selector: 'my-component',
  template: `<button (click)="clickemitter.emit($event); onClick($event)" [style.color]="color"><ng-content></ng-content></button>`
})
export class MyComponent {
  @Output clickemitter: EventEmitter<MouseEvent> = new EventEmitter();

  private color: string = "black";

  constructor() {

  }

  private onClick(event: MouseEvent): void {
    if(!event.defaultPrevented) //gets called before the observers of clickemitter
      this.color = "red";
    else this.color = "blue";
  }
}

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <my-component (clickemitter)="$event.preventDefault()">Hello Angular</my-component>
  `,
  directives: [MyComponent]
})
export class App {
  constructor() {

  }
}

I made a Plunker for this, too: https://plnkr.co/edit/yIoF1YgvkiZLBPZ6VHTs?p=preview 我也为此制作了一个Plunker: https ://plnkr.co/edit/yIoF1YgvkiZLBPZ6VHTs?p = preview

The Problem If i click on the button the color of the button wouldn't turn blue as it should but it turns red instead. 问题如果我点击按钮,按钮的颜色将不会变为蓝色,而是变为红色。 This might be because EventEmitter.emit()/next() seem to work asynchronous. 这可能是因为EventEmitter.emit()/ next()似乎异步工作。 I tried to solve this problem by also subscribing my onClick() method at the emitter and just call clickemitter.emit($event) in my button (click) event handler. 我试图通过在发射器上订阅我的onClick()方法来解决这个问题,并在我的按钮(单击)事件处理程序中调用clickemitter.emit($ event) This would work for the Plunker Demo when i did it in ngAfterInit(). 当我在ngAfterInit()中执行此操作时,这适用于Plunker演示。 But what if someone subscribes to the clickemitter later? 但是如果有人后来订阅了这个陈词滥调呢? My component would get called before that observer again and my component would be inconsistent. 我的组件将再次在该观察者之前调用,并且我的组件将不一致。

The Question How can i ensure that my component onClick() handler will get called at the very last to guarantee that no other observer can prevent the default behavior afterwards? 问题如何确保我的组件onClick()处理程序将在最后被调用,以保证其他观察者之后不能阻止默认行为? Or is there a complete different way of achieving my goal? 或者是否有完全不同的方式来实现我的目标?

To solve this problem use this in your html template 要解决此问题,请在html模板中使用此方法

<button (click)="clickemitter(a,b,c); $event.stopPropagation();" 
        [style.color]="color" >
        <ng-content></ng-content>
</button> 

As $event.stopPropagation(); 作为$event.stopPropagation(); works to stop default click event and your button only call event that written in (click)="...." which is clickemitter.emit(a,b,c) 用于停止默认点击事件和你的按钮只调用写入(click)="...."事件,这是clickemitter.emit(a,b,c)

Maybe this is help full to you. 也许这对你来说很有帮助。 Thank you. 谢谢。

Just use return false; 只需使用return false;
here is code which i have used for prevent default 这是我用来防止默认的代码

  @HostListener('click') open(){
      this._isOpen = !this._isOpen;
      return false;
  }

And for stopPropagation 并为stopPropagation

 @HostListener('click', ['$event'])
 onClick(e) {
  alert("we have performed click");
  e.stopPropagation();
 }

I implemented event.preventDefault like this 我像这样实现了event.preventDefault

export class ObserveComponent{
   onSubmit(event:Event){
     event.preventDefault();
  }
}

stopped page reload every time I tried to access value of a key which is not exist in the object 每当我尝试访问对象中不存在的键的值时,停止页面重新加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM