[英]Access element of *ngFor from component.ts
所以我正在使用angular2-swing库,并且我的模板中包含以下代码:
<ul class="stack" swing-stack [stackConfig]="stackConfig" #myswing1 (throwout)="onThrowOut($event)">
<li swing-card #restaurantCards [ngClass]="restaurant.restaurant" *ngFor="let restaurant of restaurants">
<app-restaurant-card [restaurant]=restaurant.restaurant>
</app-restaurant-card>
<div class="overlay">
YUM!
</div>
</li>
</ul>
这是onThrowOut($event)
方法:
onThrowOut(event: ThrowEvent) {
if(event.throwDirection == Direction.RIGHT){
this.restaurantService.addToLikes(event.target.getAttribute('ngClass'))
}
//make element hidden once thrown out
event.target.setAttribute("style", "visibility: hidden; transition: .1s; ");
}
但是,当我尝试获取属性时,它为null。 有什么方法可以从for循环中检索元素吗? 谢谢
ngClass
可以在DOM中访问class
。 将代码修改为(使用event.target.getAttribute('class')
):
onThrowOut(event: ThrowEvent) {
if(event.throwDirection == Direction.RIGHT){
this.restaurantService.addToLikes(event.target.getAttribute('class'));
}
...
}
另请注意,现在id
将为string
因此可能需要将其转换为number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.