angular - 使 *ngFor 为单独的 mouseover 和 mouseout 事件工作
问题描述
这是我的 Angular 代码:
<div *ngFor="let follow of followers;">
<div>{{follow.name}}</div>
<div>
<button (mouseover)="myFollow=true" (mouseout)="myFollow=false" (click)="follow(id)">
<span [hidden]="myFollow">Following</span>
<span [hidden]="!myFollow">Follow</span>
</button>
</div>
</div>
这是输出:
Joe1 Following
Joe2 Following
Joe3 Following
Joe4 Following
当我将鼠标悬停在第一行上时,所有行都变为:
Joe1 Follow
Joe2 Follow
Joe3 Follow
Joe4 Follow
它应该只改变第一行,如下所示:
Joe1 Follow
Joe2 Following
Joe3 Following
Joe4 Following
如何使(mouseover)
and(mouseout)
为单个行工作?
这是我对应的 .ts 文件
public followers = [] as [{follow_data?: any}];
export class FollowingComponent OnInit {
myFollow: boolean;
constructor() {
this.myFollow = false;
}
下面是 JSON 的样子:
"followers": [
{
"follow_data": {
"name": "joe1",
"id": "863cf135-8105-0d0df0e1c101"
},
{
"follow_data": {
"name": "joe2",
"id": "863cf135-8105-0d0df0e1c102"
},
{
"follow_data": {
"name": "joe3",
"id": "863cf135-8105-0d0df0e1c103"
},
{
"follow_data": {
"name": "joe4",
"id": "863cf135-8105-0d0df0e1c104"
},
]
解决方案
不要设置follow var,设置follow的一个属性(例如follow.following = true)
推荐阅读
- ios - UIImageView tintColor 设置,无论如何都不对图像应用颜色
- r - 更改 Shiny tableOutput 中的字体颜色
- html - 聚焦/不聚焦时过渡不起作用
- sql - 如何计算一对跨列SQL出现的次数
- android - 如何撤消 android SQLite 中的删除操作
- java - JavaFX - 无法将对象强制转换为 SimpleListProperty
- kubernetes - 通过 RBAC 限制对 Kubernetes 机密的访问
- javascript - 如果从 localhost 执行 window.name 将失去其价值
- three.js - 如何使用 Quickhull 将两个几何图形与 Three.js 相加?
- javascript - setState 的值传递给函数,直到第二个 onPress