• <bdo id='NrnEw'></bdo><ul id='NrnEw'></ul>
  1. <legend id='NrnEw'><style id='NrnEw'><dir id='NrnEw'><q id='NrnEw'></q></dir></style></legend>

      <small id='NrnEw'></small><noframes id='NrnEw'>

    1. <i id='NrnEw'><tr id='NrnEw'><dt id='NrnEw'><q id='NrnEw'><span id='NrnEw'><b id='NrnEw'><form id='NrnEw'><ins id='NrnEw'></ins><ul id='NrnEw'></ul><sub id='NrnEw'></sub></form><legend id='NrnEw'></legend><bdo id='NrnEw'><pre id='NrnEw'><center id='NrnEw'></center></pre></bdo></b><th id='NrnEw'></th></span></q></dt></tr></i><div id='NrnEw'><tfoot id='NrnEw'></tfoot><dl id='NrnEw'><fieldset id='NrnEw'></fieldset></dl></div>
      <tfoot id='NrnEw'></tfoot>

      如何将点击事件添加到打字稿中动态添加的html元

      时间:2023-09-08
    2. <legend id='K2dTj'><style id='K2dTj'><dir id='K2dTj'><q id='K2dTj'></q></dir></style></legend>
        <tbody id='K2dTj'></tbody>

            <tfoot id='K2dTj'></tfoot>
            • <bdo id='K2dTj'></bdo><ul id='K2dTj'></ul>
              <i id='K2dTj'><tr id='K2dTj'><dt id='K2dTj'><q id='K2dTj'><span id='K2dTj'><b id='K2dTj'><form id='K2dTj'><ins id='K2dTj'></ins><ul id='K2dTj'></ul><sub id='K2dTj'></sub></form><legend id='K2dTj'></legend><bdo id='K2dTj'><pre id='K2dTj'><center id='K2dTj'></center></pre></bdo></b><th id='K2dTj'></th></span></q></dt></tr></i><div id='K2dTj'><tfoot id='K2dTj'></tfoot><dl id='K2dTj'><fieldset id='K2dTj'></fieldset></dl></div>

              • <small id='K2dTj'></small><noframes id='K2dTj'>

              • 本文介绍了如何将点击事件添加到打字稿中动态添加的html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在用 Angular 2 构建一个应用程序.我想将点击事件添加到动态添加的 html 元素中.我定义了一个字符串(contentString),在这个字符串中我定义了html元素.

                I'm building an app in angular 2. I want to add a click event to a dynamically added html element. I define a string (contentString), and in this string I define the html element.

                var contentString = '<b>' + this.mName + '</b><br/> ' + this.mObject.category + '<br/> Click here for more information <button (click)="navigate()">Navigate here</button>'; 
                

                这个字符串被放在一个像这样的html元素中:

                This string is put inside a html element like this:

                var boxText = document.createElement("div");
                    boxText.innerHTML = contentString;
                

                虽然当我检查元素时,它定义了点击事件,但它没有触发.

                Although when I inspect the element, it has the click event defined, but it does not trigger.

                点击它应该控制台日志

                navigate() {
                console.log("eeeehnnananaa");
                }
                

                但这不起作用.有人解决吗?

                But that does not work. Anyone a solution?

                推荐答案

                Angular 在组件编译时处理模板.以后添加的 HTML 不再编译,绑定被忽略.

                Angular processes the template when the component is compiled. HTML added later is not compiled anymore and bindings are ignored.

                你可以使用

                constructor(private elRef:ElementRef) {}
                
                ngAfterViewInit() {
                  // assume dynamic HTML was added before
                  this.elRef.nativeElement.querySelector('button').addEventListener('click', this.onClick.bind(this));
                }
                

                这篇关于如何将点击事件添加到打字稿中动态添加的html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Angular 2 - 从承诺中返回 HTTP 下一篇:如何删除输入类型中的默认颜色?

                相关文章

                最新文章

                  <bdo id='gNXqQ'></bdo><ul id='gNXqQ'></ul>
              • <legend id='gNXqQ'><style id='gNXqQ'><dir id='gNXqQ'><q id='gNXqQ'></q></dir></style></legend>

                <tfoot id='gNXqQ'></tfoot>
              • <small id='gNXqQ'></small><noframes id='gNXqQ'>

                    <i id='gNXqQ'><tr id='gNXqQ'><dt id='gNXqQ'><q id='gNXqQ'><span id='gNXqQ'><b id='gNXqQ'><form id='gNXqQ'><ins id='gNXqQ'></ins><ul id='gNXqQ'></ul><sub id='gNXqQ'></sub></form><legend id='gNXqQ'></legend><bdo id='gNXqQ'><pre id='gNXqQ'><center id='gNXqQ'></center></pre></bdo></b><th id='gNXqQ'></th></span></q></dt></tr></i><div id='gNXqQ'><tfoot id='gNXqQ'></tfoot><dl id='gNXqQ'><fieldset id='gNXqQ'></fieldset></dl></div>