简体   繁体   English

点击事件在 innerhtml 字符串 angular 6 中不起作用

[英]click event is not working in innerhtml string angular 6

I am working on an generate dynamic template using angular 6. I have an API that return strings like below:我正在使用 angular 6 生成动态模板。我有一个返回如下字符串的 API:

    <button type="button" (click)="openAlert()">click me</button>

and html和 html

    <div [innerHtml]="myTemplate | safeHtml">
      </div>

function is bellow: function 如下:

    openAlert() {
        alert('hello');
      }

You cannot bind angular events directly to innerHTML.您不能将 angular 事件直接绑定到 innerHTML。

Still if you need to attach the event listeners you need to to do it after the html content is loaded.尽管如此,如果您需要附加事件侦听器,则需要在加载 html 内容后执行此操作。

Once the content is set to the variable, ngAfterViewInit Angular life cycle event will be triggered.一旦将内容设置为变量,就会触发 ngAfterViewInit Angular 生命周期事件。 Here you need to attach the required event listeners.在这里,您需要附加所需的事件侦听器。

Checkout the working example below.查看下面的工作示例。

component.html组件.html

<button (click)="addTemplate()">Get Template</button>
<div [innerHTML]="myTemplate | safeHtml"></div>

component.ts组件.ts

import { Component, ElementRef } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  myTemplate  = '';

  constructor(private elementRef:ElementRef){

  }
  openAlert() {
    alert('hello');
  }
  addTemplate(){
     this.myTemplate  = '<button type="button" id="my-button" (click)="openAlert()">click mee</buttn>';
  }
  ngAfterViewChecked (){
    if(this.elementRef.nativeElement.querySelector('#my-button')){
      this.elementRef.nativeElement.querySelector('#my-button').addEventListener('click', this.openAlert.bind(this));
    }
  } 
}

safe-html.pipe.ts安全-html.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
  name: 'safeHtml'
})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
  transform(value) {
        return this.sanitized.bypassSecurityTrustHtml(value);
    }

}

this should work too:这也应该有效:

component.html组件.html

<div #template></div>

component.ts组件.ts

@ViewChild('template') myTemplate: ElementRef;

addTemplate(){
   this.myTemplate.nativeElement.innerHTML = '<button type="button">click me</button>';
   this.myTemplate.nativeElement.addEventListener('click', this.openAlert);
}

Basically this will not work.基本上这行不通。 When you write code in Angular, it is transpiled by webpack and converted to javascript that is executed in the browser.当您在 Angular 中编写代码时,它会被transpiled转译并转换为在浏览器中执行的 javascript。

However, now you are injecting Angular code dynamically and not build ing it.但是,现在您正在动态注入Angular code而不是build它。 The event detection (click) would not work natively and the function openAlert is also not in the global scope where it is injected.事件检测(click)本身无法正常工作,并且 function openAlert也不在注入它的全局 scope 中。 You will have to consider a different approach and generate content using <ng-template> based on response from the API .您将不得不考虑不同的方法并根据来自API的响应使用<ng-template>生成内容。

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

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