我想要ngFof中的单击元素显示由ngIf隐藏的信息。 现在,单击图像,将显示所有隐藏的元素。 如何显示单击图像的信息?如何在* ngFor列表中隐藏其他使用Angular 2的单击元素
我没有在我的例子中使用jquery,因为我无法让它工作。 我正在寻找如何在接受其他解决方案之前在angular2中做到这一点。 plunker
@Component({
selector: 'hello-world',
template: 'src/hello_world.html',
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class HelloWorld {
clicked() {// only show clicked img info
e=event.target;
console.log(e);
this.show=!this.show;
};
info = [{
data: "information for img1:",
data2: "only the info img1 is displayed"
},
{
data: "information for img2:",
data2: "only the info for img2 is displayed"
},
{
data: "information for img3:",
data2: "only the info for img3 is displayed"
}]
}
<div *ngFor="#x of info">
<img src="http://lorempixel.com/150/150" (click)="clicked($event)" >
<div *ngIf="show">
<div class="names">
<div class="fullName">{{x.data}}</div>
<div>{{x.data2}}</div>
</div>
</div>
</div>
我发现这个职位有虽然我不能在我的代码实现它类似的问题。 angular2-get-reference-to-element-created-in-ngfor
@JS_astronauts:我已经更新了我的答案 – dieuhd
如果“事物”数据来自外部资源,如何在不向“things”数组添加“show:false”属性的情况下完成此操作? @dieuhd你有一个很好的解决方案,但打开的信息不会在选择其他人时关闭。 –
@JS_astronauts:你不需要''东西'中的declera'show'。 – dieuhd