【发布时间】:2020-09-28 09:30:00
【问题描述】:
我有一个带有列表的 html 组件
<div *ngFor="let item of myArr">
<ul>
<li>
{{ item }}
<span>
<mat-icon (click)="expression($event)">add</mat-icon>
</span>
<ng-container
><h2 *ngFor="let item of textArr">
{{ item }}
</h2></ng-container
>
</li>
</ul>
</div>
和带有数组的组件 ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss'],
})
export class ChildComponent implements OnInit {
myArr = ['1', '2', '3', '4'];
abc = ['a', 'b', 'c', 'd'];
textArr = [];
constructor() {}
ngOnInit(): void {}
expression(ev: Event) {
console.log(ev);
this.textArr.push(this.abc);
}
}
我该怎么做,当我点击 + 1 前面的内容时,'a' form abc 将仅显示在 1 下,如果我点击 + 2 前面的内容,那么 abc 中的 b 应该是显示在 2 下等...
【问题讨论】:
-
会有多少表格?如果很少有表单在点击时使用 ng-hide/show 或 ng-if 并使用 ngFor 的索引来引用 abc 数组索引。
-
会有很多表格,我发布我的任务的简化版本。
-
好的,myArr 数组,是不是只能引用到 abc?
-
是的,它就在那里
-
好的,我有一个适合你的例子,希望能指引你正确的方向。
标签: javascript html arrays angular typescript