Angular 2 child component receive ancestor event

前端 未结 1 901
不思量自难忘°
不思量自难忘° 2021-01-13 15:34

I have an Angular component that depends on a click event that occurs on the root App component. The child is located in random places inside the co

相关标签:
1条回答
  • 2021-01-13 15:40

    For "click outside", try this:

    @Component({
      selector: 'child',
      template: '<div (click)="childClick($event)">child content here</div>',
      host: { '(document:click)': 'docClick()' }
    })
    export class Child {
      docClick() {
        console.log('clicked outside');
      }
      childClick(ev) {
        console.log('child click');
        ev.stopPropagation();
      }
    }
    

    Plunker

    Update: Eric just posted the following in another answer:

    "You can use listenGlobal that will give you access to document, body, etc.

    renderer.listenGlobal('document', 'click', () => {
      console.log('Document clicked');
    });
    
    0 讨论(0)
提交回复
热议问题