(click)事件在innerHtml字符串Angular 4中不起作用 [英] (click) event not work in innerHtml string Angular 4

查看:579
本文介绍了(click)事件在innerHtml字符串Angular 4中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击<a...标记时未调用我的函数.

My function isn't called when I click the <a... tag.

我的组件中包含以下代码:

I have the following code in my component:

public htmlstr: string;
public idUser:number;

this.idUser = 1;
this.htmlstr = `<a (click)="delete(idUser)">${idUser}</a>`;

public delete(idUser){
    alert("id " + idUser);
}

我的html

<div [innerHTML]="htmlstr"></div>

但未调用功能delete并且不显示警报.

but the function delete isn't called and does not show the alert.

<div...是动态创建的

推荐答案

如果有人遇到相同的问题,并且最重要的是答案不起作用,请尝试以下技巧:

If anyone face same issue and above all answer not working then try my trick :

在HTML中:

<button onclick="Window.myComponent.test()"> test </button>

在组件中:

class 
  constructor(){
    Window["myComponent"] = this;
  }


  test(){
    console.log("testing");
  }

这篇关于(click)事件在innerHtml字符串Angular 4中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆