Div的Onclick在Angular上不起作用-9

用户名

这是我的HTML

<mat-grid-list cols="3" rowHeight="150px">
 <mat-grid-tile
    *ngFor="let tile of tiles;index as j;"
     [colspan]="tile.cols"
     [rowspan]="tile.rows"
     >
   <div (onclick)="openbox();" style="z-index: 2">
                                       
          {{tile.text}}<br>
          <img src="https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1027&q=80" height="200px" width="260px">
   </div>
    </mat-grid-tile>
</mat-grid-list>

s

export class RegistrationComponent implements OnInit {
...
 openbox(){
    console.log('clicked')
  }
...
}

我尝试过的事情:

style="z-index: 2; position: relative;"
隐身

浏览器Chrome Angular-9
我的目标是获取单击的mat-grid-tile的索引,还有其他方法吗?
谢谢

用户1

您应该使用(单击)事件。onClick之所以不起作用是因为Angular编译器无法识别它,并且对于所有带prefix的浏览器事件都是这样on如果您不想在Angular中使用该事件,则需要删除on

<div (click)="openbox();" style="z-index: 2">
   ...
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章