首页 > 解决方案 > Angular:将函数从常量绑定到html

问题描述

在 Angular 中,我将一些模板定义为常量,模板渲染得很好,但我想将事件附加到 html 常量中的元素。我似乎无法像在@Component 定义中定义模板时那样绑定函数。

这是一段描述我尝试过的代码。

export class SomeComponent implements OnInit {
    public ngOnInit(): void {
        const someHtml = `
          <a (click)="someFunction()" href="someUrl">link</a>
        `
        .. some code that uses the const
    }

    public someFunction() {
        console.log("clicked")
    }
}

我该怎么办?

标签: htmlangularfunctiontypescript

解决方案


推荐阅读