繁体   English   中英

从 typescript 文件中存在的 html 元素调用 function(角度)

[英]Calling a function from an html element present inside a typescript file (Angular)

我正在根据后端的特定条件渲染一个按钮。 该按钮是从 ts 文件 home.component.ts 中呈现的

if(backendLogic is true)
{
const buttonz = '<button onClick="xyz()"> Click me</button>';
return buttonz; 
}


xyz()
{
  console.log("haha");
}

现在当点击按钮时,它给出了一个错误,说 UncaughtReference: xyz is not defined。 xyz() 存在于同一个 typescript 文件中。 如何从这个 html 元素中调用这个 function? 我正在使用 Angular。

你应该使用 *ngIf 指令:

组件.html:

<button *ngIf="backendLogic" onClick="xyz()">Click me</button>

组件.ts:

xyz()
{
  console.log("haha");
}

使用此方法,只有当backendLogictrue时,按钮才会在 DOM 中呈现。

暂无
暂无

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

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