【问题标题】:How can I insert an HTML comment with an interpolated expression?如何插入带有插值表达式的 HTML 注释?
【发布时间】:2015-01-30 21:54:25
【问题描述】:

我想在 ng-repeat 块中添加带有插值表达式的 HTML 注释。但是,当我尝试这样做时,表达式不会被插值。例如:

<tr ng-repeat="item in items"
    <!-- ID that I don't want the user to see, but want for debugging = {{item.id}} -->
    <td>{{item.prettyName}}</td>
    <td>{{item.someProperty}}</td>
    <td>{{item.someOtherProperty}}</td>
</tr>

当我查看 DOM(即 Chrom DevTools 中的元素选项卡)时,我只看到未插值的字符串 ("{{item.id}}") 而不是插值。

这里的正确语法是什么?

【问题讨论】:

  • divspan 上使用display:noneng-show/ng-hide 怎么样?
  • 我的回答解决了你的问题吗?
  • 是的,谢谢 - 确实有点矫枉过正,但非常有帮助。谢谢!

标签: angularjs


【解决方案1】:

这太过分了,因为您可以使用display: none 或类似的 cmets 中的建议,但这只是一个有趣的练习:

在注释上调用指令的语法是:

<!-- directive: foo expression -->

我希望像 ng-bind 这样的东西支持 cmets,但事实并非如此。但您可以轻松创建自己的:

app.directive('comment', function($interpolate) {
  return {
    restrict: 'M',
    scope: true,
    link: function(scope, element, attrs) {
      var comment = $interpolate(attrs.comment)(scope);
      element.replaceWith("<!-- " + comment + "-->" );
    }
  };
});

而用法是:

<!-- directive: comment "something something {{item.id}}" -->

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多