kittencup / angular2-ama-cn

angular2 随便问
691 stars 101 forks source link

<img (click)="setImg()" src="1.jpg"> 如何点击这个图,就获取到图片地址 #174

Closed ghost closed 8 years ago

ghost commented 8 years ago

<img (click)="setImg()" src="1.jpg">

我想点击这个图,就把这个图对应的地址,传送给setImg 这个函数

我试着传入 $event,似乎是可以获取到图,但是好像太麻烦了

有更加简便的办法吗

ghost commented 8 years ago

解决了 http://stackoverflow.com/questions/36006894/angular2-get-clicked-element-id

ShiYuanjun-Tim commented 8 years ago

@iloveyo123u1 hey actually the best solution is using template reference variable, following is the change from your plnkr code , you can directly get the id .

    `<button (click)="onClick(ff.id)" id="test" #ff>Click</button>`
ghost commented 8 years ago

thank you @ShiYuanjun-Tim