我想要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