【发布时间】:2019-08-20 19:59:48
【问题描述】:
我正在使用指令*ngFor 将离子卡组件从名为userData 的数组加载到DOM。 userData 数组由具有 id、title 和 count 属性的嵌套对象组成。
我正在尝试创建一个点击/点击事件,每当用户点击卡片时,计数就会增加被点击的特定卡片。
我遇到的问题是当我点击卡片时*ngFor 没有更新。但是当我只引用 *ngFor 指令之外的计数时,它确实有效。
我想使用*ngFor 指令并在我单击每张卡片时单独更新它们。任何帮助深表感谢。
Tally 组件 HTML 模板粘贴
<ion-card class="ion-card-custom" (click)="increaseCount()" *ngFor="let tal of userData">
<ion-item>
<ion-icon name="pin" slot="start" ></ion-icon>
<ion-label>{{ tal.title }}</ion-label>
<section class="tally-info-count" >{{ tal.count }} </section>
<section class="tally-info-count" >{{ tap }} </section>
<ion-button fill="outline" slot="end" class="info-button"> i </ion-button>
</ion-item>
<ion-item>
<ion-label>{{ tal.created }} </ion-label>
<p (click)="increaseCount()">{{ plus }} </p>
</ion-item>
</ion-card>
<ion-card (tap)="tapEvent($event)">
<ion-item>
Tapped: {{tap}} times
</ion-item>
</ion-card>
完整的 Tally 组件粘贴
import { getLocaleDateTimeFormat } from '@angular/common';
//decorator
@Component({
selector: 'app-tally',
templateUrl: 'tally.html',
styleUrls: ['tally.scss'],
})
export class Tally implements OnInit {
title: string;
public count: number = 0;
public tap: number = 0;
plus: number;
userData: any[];
id: number;
//date created
dateTime: Date = new Date();
created: string = `${this.dateTime.getUTCMonth() +1}/${this.dateTime.getDay}/${this.dateTime.getUTCFullYear()}`
constructor() { this.plus = 0;}
ngOnInit() {
this.title = "Title";
this.userData = [
{
id: this.id,
title: this.title,
count: this.tap,
created: this.created
},
{
id: this.id,
title: this.title,
count: this.count,
created: this.created
}
]
}
increaseCount () {
this.count += 1;
this.plus += 1;
console.log('this.count:', this.count);
}
tapEvent(e) {
console.log(e);
this.tap++;
}
}
【问题讨论】:
-
这篇文章让我有点困惑..你能改写一下这个问题还是重申一下?
-
是的。对不起,我会改写。
-
我改写了它,但我还要在这里留下一个小得多的问题。我真正想做的基本上是每次点击或点击卡片时都会增加每张卡片。我希望每张卡片只有在被点击时才能增加,而不是任何其他卡片。我希望这有帮助。并提前感谢您!
标签: angular ionic-framework ionic4 angular8