我想访问放置指令的下一个 div。
我的指令
import {Directive, HostBinding, HostListener} from '@angular/core';
@Directive({
selector:'[tDropdown]'
})
export class DropdownDirective{
@HostBinding('class.hidden') get opened(){
return this.isOpen;
}
@HostListener('click') open(){
this.isOpen = false ;
}
@HostListener('mouseleave') close(){
this.isOpen = true;
}
private isOpen = true;
}
<div class="well box" tDropdown>
<span>Group :-</span>{{entry.key}}
<div> // access this div from the directive
<table class="table">
<thead class="thead-inverse">
<tr>
如何从放置指令的元素访问后续的 div?
最佳答案
class DropdownDirective {
constructor(private elRef:ElementRef) {}
ngAfterContentInit() {
var div = this.elRef.nativeElement.querySelector('div')
div.classList.add('hidden');
}
如果要在父组件上做
@ViewChild(DropdownDirective, {read: ElementRef}) elRef:ElementRef;
click() {
var div = this.elRef.nativeElement.querySelector('div')
div.classList.add('hidden');
}
关于angular - 访问主机绑定(bind) Angular2 中的下一个 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42190068/