How to access any of the element under a directive and add events?

孤者浪人 提交于 2019-12-13 04:42:22

问题


Here is my directive:

import { Directive, HostListener, ElementRef, Renderer, Input } from '@angular/core';

@Directive({
  selector: '[carouselDirective]'
})
export class CarouselDirective {

    @Input() carouselDirective:any;

    constructor( private el : ElementRef, private renderer: Renderer ) { 
        console.log(el.nativeElement); //how to add the click / hover events to it's childrens?
    }

    @HostListener('click') onClick() {
        window.alert("onClick");
    }

    @HostListener('mouseover') onHover() {
        window.alert("hover");
    }

}

My question is :

1. how to add any of the events to any of the children of my directive? ( on event i require to add and remove some class names )
2. how to communicate with his parent component?
3. how to get some value from parent component and manipulate in directive?
4. can't I add remove class name to child elements?
5. on click of child element can't i change property of parent element?
6. if there is 5 element under a parent to add events to them, required to create separate 5 directive(s"?

来源:https://stackoverflow.com/questions/50088002/how-to-access-any-of-the-element-under-a-directive-and-add-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!