只需添加一个输入类型检查并使用[(ngModel)]
<ul class="list-group" *ngFor="let x of inf | keyvalue">
<li class="list-group-item">
<!--add a input type checkbox and relation with x.check-->
<input type="checkbox" [(ngModel)]="x.check">
{{x.key}}</li>
<!---change the *ngIf to add the x.check condition-->
<ng-container *ngIf="x.check && x.value.hasOwnProperty('properties')">
<ul *ngFor="let y of x.value.properties | keyvalue">
<li>
{{y.key}}
</li>
</ul>
</ng-container>
</ul>
更新
如果你想要一个“递归组件”,这很容易。我举一个例子,你可以在
stackblitz
data = [{
title: "uno", children: [
{ title: "uno-uno" }]
},
{
title: "dos", children: [
{ title: "dos-uno",children: [
{ title: "dos-uno" }
]},
{ title: "dos-dos" }]
}
]
我们可以有一个app.component,比如
<li *ngFor="let item of data">
<app-li [title]="item.title" [children]="item.children"></app-li>
</li>
我们的应用程序李喜欢
<li (click)="check=!check">
<div [className]="children?check?'arrow-down':'arrow-up':'arrow-right'"></div>
{{title}}
</li>
<ul *ngIf="children && check">
<ng-container *ngFor="let item of children">
<app-li [title]="item.title" [children]="item.children"></app-li>
</ng-container>
</ul>
请确保我们在应用程序li中添加“children”
注意:我添加了一个<部门>使用className“模拟”三角形
更新2
我们可以自己传递自己的项目
组件变得像
@Component({
selector: 'app-li',
template: `<li >
<div (click)="check=!check" [className]="item.children?
check?'arrow-down':'arrow-up':'arrow-right'">
</div>
<input type="checkbox" [(ngModel)]="item.select" >
<span (click)="check=!check">{{item.title}}</span>
</li>
<ul *ngIf="item.children && check">
<ng-container *ngFor="let item of item.children">
<app-li [item]="item" ></app-li>
</ng-container>
</ul>
`,
styleUrls: [ './hello.component.css' ]
})
export class HelloComponent {
@Input() item: any;
}
<li *ngFor="let item of data">
<app-li [item]="item" ></app-li>
</li>
stackblitz forked