[英]Angular card arrays with *ngfor
I am on a self learning road right now teaching myself angular.我现在正在自学angular。
For my project that i am working on, i am using some card elements, i created an interface for the array with another array in it.对于我正在处理的项目,我正在使用一些卡片元素,我为数组创建了一个接口,其中包含另一个数组。
The ts: ts:
categories: categorieCards[] = [
{
title: 'animals',
image: '../../assets/Images/animals/main.jpg',
children: [
{
title: 'Pig',
image: '#',
children: []
so as you can see i createt categories[] which has another children[] in it to create more objects.如您所见,我创建了 category[],其中包含另一个 children[] 以创建更多对象。 But right now i am only abel to display the first categories but not the children.但现在我只是 abel 显示第一个类别而不是孩子。
the html: html:
<div class="card-wrapper" fxLayout="row wrap" fxLayoutGap="1rem" fxFlexOffset=" 10px"
fxFlex="100">
<div class="cardslayout mat-elevation-z4" *ngFor="let category of categories" fxFlex="calc(50% - 16px)">
<img [src]="category.image">
<span>
{{category.title}}
</span>
</div>
</div>
So here my question: how can i make the card element clickable, so the children array will be displayed as the parent cards.所以我的问题是:我怎样才能使卡片元素可点击,所以子数组将显示为父卡片。
You may have different options to implement this feature: 1- first option is create a component for your category and inside it create a variable and make it true or false when user clicks on header and show the chid results with *ngFor
您可能有不同的选项来实现此功能: 1- 第一个选项是为您的类别创建一个组件,并在其中创建一个变量并在用户单击 header 并使用*ngFor
显示 chid 结果时使其为真或假
<div class="card-wrapper" fxLayout="row wrap" fxLayoutGap="1rem">
<div class="cardslayout mat-elevation-z4" *ngFor="let category of
categories" fxFlex="calc(50% - 16px)">
<your-category-compoennt [category]="category"> <your-category-component>
</div>
</div>
then in your your-category-component.html
:然后在your-category-component.html
:
<div (click)="showClidren = true">
<img [src]="category.image">
<span>
{{category.title}}
</span>
</div>
<div *ngIf="showClidren" >
<span *ngFor="let child of
category"> </span>
</div>
then in your your-category-component.ts
:然后在your-category-component.ts
:
export class YourCategoryComponent {
@Input()category
showClidren = true
...
}
2- next option is to add a show showChildern: boolen
to your model or interface and by clicking in the title make it true and show children with ngfor
in the same component and ngIf
2-下一个选项是添加一个 show showChildern: boolen
到您的 model 或界面,并通过单击标题使其为真并在同一组件中显示带有ngfor
的子级和ngIf
3- use Ui libraries like mat-expansion-panel angular mterial 3-使用 Ui 库,如mat-expansion-panel angular 材料
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.