简体   繁体   English

如何在 Angular 6 中动态创建的按钮上绑定点击事件?

[英]How to bind on click event on dynamically created button in Angular 6?

I am trying to create a dynamic button with an onclick event.我正在尝试创建一个带有 onclick 事件的动态按钮。 The showname() defined the on same Component.ts. showname() 在同一个 Component.ts 上定义。 But there is no response on clicking the button但是点击按钮没有反应

Component.ts组件.ts

createtooltip() {
  this.tooltip = document.createElement('div');
  this.tooltip.style.cssText =
    'position:absolute; background:black; color:white; padding:4px;z-index:10000;' +
    'border-radius:2px; font-size:12px;box-shadow:3px 3px 3px rgba(0,0,0,.4);' +
    'opacity:0;transition:opacity 0.3s';
  this.tooltip.innerHTML = '<button id="popup" (click)="showname()" >Copy!</button>';
  document.body.appendChild(this.tooltip);
}

showname() {
  console.log("Hi User");
}

Could anyone help me to find the solution?谁能帮我找到解决方案?

You won't have access to the document object everywhere.您将无法在任何地方访问文档对象。

So, you shouldn't be using document functions to do DOM manipulations.因此,您不应该使用document函数来进行 DOM 操作。 All these DOM Manipulations should be done only using Rendere2.所有这些 DOM 操作应该只使用 Rendere2 来完成。 If there's anything that you want to access on the DOM, you should do it using @ViewChild如果您想在 DOM 上访问任何内容,您应该使用@ViewChild

Here's an Example:这是一个例子:

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

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

  constructor(private renderer: Renderer2) {}

  createtooltip() {
    this.renderer.setAttribute(this.tooltip.nativeElement, 'class', 'my-button');

    const button = this.renderer.createElement('button');
    this.renderer.setProperty(button, 'id', 'popup');
    this.renderer.setProperty(button, 'innerText', 'Copy');
    this.renderer.listen(button, 'click', (event) => {
      this.showname();
    })

    this.renderer.appendChild(this.tooltip.nativeElement, button);
  }

  showname() {
    console.log("Hi User");
  }

}

In template:在模板中:

<button (click)="createtooltip()">Create Tooltip</button>

<div #tooltip>

</div>

In CSS:在 CSS 中:

p {
  font-family: Lato;
}

.my-button {
  position:absolute;
  background:black;
  color:white;
  padding:4px;
  z-index:10000;
  border-radius:2px;
  font-size:12px;
  box-shadow:3px 3px 3px rgba(0,0,0,.4);
  opacity:0;
  transition:opacity 1s linear;
}

Here's a Sample StackBlitz for your reference.这是一个示例 StackBlitz供您参考。

I solved this problem by the other way我用另一种方式解决了这个问题

<a ngFor="let link of links" (click)="actions[link]()">Click</a>

` actions: any = { ` 动作:任何 = {

link1: () => this.func1(),
link2: () => this.func2()

} ` }`

angular doesnt compile the dynamic created HTML elements . angular 不会编译动态创建的 HTML 元素。 u have to use ng-template like this :你必须像这样使用ng-template

<ng-template #myTemplate>
    <div styles="...">
       <button id="popup" (click)="showname()" >Copy!</button>
    </div>
</ng-template>

It would be better to create the button with directs like *ngFor or *ngIf rather than create the elements like you do with Jquery .最好使用像*ngFor*ngIf这样的*ngFor创建按钮,而不是像使用Jquery那样创建元素。

This is due to the nature of Angular, which prioritizes and eases the usage of directives over simple javascript.这是由于 Angular 的性质,它优先考虑并简化指令的使用,而不是简单的 javascript。

to do this you can :要做到这一点,你可以:

HTML : HTML :

<button id="popup" (click)="showname()" *ngIf='elements.showNameButton==true' >Copy!</button>

** TS :** ** TS :**

elements={
 showNameButton:false
}

createtooltip(){
 this.elements.showNameButton =true;
}

showname() {
  console.log("Hi User");
}

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

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