简体   繁体   English

Angular document.getElementById 不能动态使用插值

[英]Angular document.getElementById not working with interpolation dynamically

I have a list of dynamic images that I want to display opacity only when the user hovers over the particular image.我有一个动态图像列表,我只想在用户将鼠标悬停在特定图像上时才显示不透明度。 My issue is I assign each image an id dynamically but cannot get the property of the element dynamically.我的问题是我为每个图像动态分配了一个 id,但无法动态获取元素的属性。

I get and error of Got interpolation ({{}}) where expression was expected我得到了Got 插值 ({{}}) 的错误,其中表达式是预期的

<span *ngFor="let image of imagess">   
  <img  attr.id="Image{{image.id}}"
    [src]="sanitizer.bypassSecurityTrustUrl('data:'+image.mimeType+';base64, '+image.frontImage)"       
    onmouseover="style.opacity=.16;"
    onmouseout="style.opacity=1;"
    />    
  <span onmouseover="document.getElementById('Image'{{image.id}}).style.opacity=.16;"> <----Right here is what I need 

  //Icons and other things here      
  </span>    
</span>

I would rewrite it to Angular way:我会将其重写为 Angular 方式:

<span *ngFor="let image of images">   
  <img
    ...
    #img   
    (mouseover)="img.style.opacity= '.16'"
    (mouseout)="img.style.opacity= '1'"
    />    
  <span 
    (mouseover)="img.style.opacity= '.16'"
    (mouseout)="img.style.opacity= '1'"
  >Hover over me</span>    
</span>

Ng-run Example Ng-run 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM