【问题标题】:How to use interpolation to set an attribute selector?如何使用插值来设置属性选择器?
【发布时间】:2016-07-29 03:26:26
【问题描述】:

我有一个 String 变量,它包含对图标的引用,我想将其绑定到 HTML 中以显示它,作为属性选择器。


代码片段

.html

    <thumbnail {{ obj.getIcon() }}> </thumbnail>

.ts

    getIcon() {
        return "icon-obj" + this.id;
    }

输出

    <thumbnail "icon-obj1"> </thumbnail>

期望的输出

    <thumbnail icon-obj1> </thumbnail>

已知和不受欢迎的替代解决方案

    <thumbnail class={{ obj.getIcon() }} > </thumbnail>

基本上,字符串引号会搞砸一切。如果我使用不同类型的选择器,它会起作用,就像我在上面的示例中显示的那样输出到class="icon-obj1",但这不是重点。

那么,有什么建议吗? 感谢阅读!

【问题讨论】:

    标签: html css angularjs typescript angular


    【解决方案1】:

    没有办法做到这一点。对于动态添加的属性或类,无论如何都不会创建指令或组件 - 仅适用于静态添加的元素名、属性名和类名。

    要动态添加组件,请使用DynamicComponentLoader
    我还不知道这是否也适用于指令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 2018-02-06
      • 2011-08-06
      • 1970-01-01
      • 2010-12-30
      • 2021-01-31
      • 2023-02-14
      相关资源
      最近更新 更多