How do I access [attr.open]="false" from the div "child_div". In short I want to emulate the code sample below, but I don't know how to access parent attr from child div.
<div class="parent_div" *ngFor="let level_1 of [1,2,3]" [attr.open]="false">
<div class="chlid_div" *ngIf="parent.attr.open">
<div class="parent_div" *ngFor="let level_2 of [1,2,3]"
[attr.open]="false">
<div class="chlid_div" *ngIf="parent.attr.open">
content
</div>
</div>
</div>
</div>
You can make use of the Following Template
<div [attr.open]="my()">
<div class="chlid_div" *ngIf="data">content</div>
</div>
Component
export class AppComponent {
data: boolean;
my() {
this.data = true;
}
}
Working Example
Ok the solution was actually pretty simple, no directives or editing in component needed, pure template solution.
<div #areaLevel class="parent_div" *ngFor="let level_1 of [1,2,3]">
<div class="chlid_div" [hidden]="areaLevel.expanded">
<div class="button" [class.closed]="areaLevel.expanded" (click)="areaLevel.expanded = !areaLevel.expanded">
+
</div>
<div #areaLevel class="parent_div" *ngFor="let level_2 of [1,2,3]">
<div class="chlid_div" [hidden]="areaLevel.expanded">
...this can continue indefenitly...
</div>
</div>
</div>
</div>
You can use either *ngIf or [hidden], like I did, but with *ngIf it destroys the DOM and if you open it again it builds it from 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With