【问题标题】:html binding of a static property静态属性的html绑定
【发布时间】:2019-11-15 22:49:01
【问题描述】:

我有一个由组件和模板组成的模块。 在组件中我有一个静态函数(我需要它是静态的,因为我正在制作一个库),所以我还有一个静态属性,它将包含一个 html 字符串。

我正在尝试使用 [innerhtml] 将此属性绑定到我的模板。

组件.ts:

导出类 AppSqvComponent {

static sqv: string;


static myFunc(): void {

        this.sqv = myHtmlString;

}

模板.html:

模板无法识别“sqv”,表示变量未解析。

【问题讨论】:

  • 我认为answer 可以解决您的问题。
  • 问题已解决:在我的 ts.config.json 中,我必须设置“module”:“esnext”和“target”:“es2015”。在我使用 es5 之前。我不完全理解,但我认为这是编译过程中的一个问题,也许我使用了一些与我在配置中的设置不兼容的旧语法......有了这个我可以在不使用 static 关键字的情况下实现我的函数和属性,然后能够以经典方式进行 html 绑定。还是谢谢你的帮助!!!

标签: angular typescript components angular2-template


【解决方案1】:

您的“sqv”属性是静态的(属于一个类)。由于这个原因,你不能将它与“this”一起使用......“this”表示它属于一个已经创建的实例而不是一个类......

export class AppSqvComponent {
    static sqv: string;
    static myFunc(): void {
            this.sqv = myHtmlString; //===> wrong
    }

你必须像这样使用它:

export class AppSqvComponent {
    static sqv: string;
    static myFunc(): void {
            AppSqvComponent.sqv = myHtmlString; //===> Ok
    }

【讨论】:

    猜你喜欢
    • 2014-09-10
    • 2010-10-30
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2012-10-15
    • 2013-08-17
    • 2014-01-29
    相关资源
    最近更新 更多