I'm trying to create a Structural Directive that will behave similarly to the RouterLinkActive directive.
So I implemented the UnlessDirective example and added the parts from the Angular's RouterLinkActive directive where it obtains the children through the ContentChildren decorator.
// TODO(issue/24571): remove '!'.
@ContentChildren(RouterLink, { descendants: true })
links !: QueryList<RouterLink>;
// TODO(issue/24571): remove '!'.
@ContentChildren(RouterLinkWithHref, { descendants: true })
linksWithHrefs !: QueryList<RouterLinkWithHref>;
My template looks like this:
<p *appUnless="false">
This paragraph is displayed because the condition is false.
<a routerLink="/another-page">my link</a>
</p>
And I tried to access the links with:
ngAfterContentInit(): void {
console.log("links length", this.links.length);
console.log("linksWithHrefs length", this.linksWithHrefs.length);
}
The problem is, the links and linksWithHrefs variables are never filled with any contents. What am I missing? Full code in this stackblitz. I'm using Angular 7.