[英]viewChildren with directive
I want to add a directive and use viewChildren for identify them: 我想添加一个指令并使用viewChildren来标识它们:
// directive.ts
@Directive({
selector: '[myOwnDirective]'
})
export class MyOwnDirective {
constructor() {
console.log("hi") //never printed
}
}
//component html:
<div myOwnDirective>content 1</div>
<div>content 2</div>
<div>content 3</div>
<div myOwnDirective>content 4</div>
<button (click)="printElements()"></button>
//component.ts:
export class MyOwnComponent implements AfterViewInit{
@ViewChildren(MyOwnDirective) children: QueryList<MyOwnDirective>;
constructor() {
}
ngAfterViewInit() {
}
printElements(){
console.log(this.children) // Empty
}
What I want is get all elements with "myOwnDirective" but I can't find how. 我想要的是使用“ myOwnDirective”获取所有元素,但我找不到方法。
where is my mistake? 我的错误在哪里?
您需要在app.module
中注册指令才能在您的应用程序中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.