我怎么可以插入一个插值前pression HTML注释? [英] How can I insert an HTML comment with an interpolated expression?
问题描述
我想用NG-重复块内插前pression添加HTML注释。然而,当我尝试这样做,前pression不插。例如:
I'd like to add an HTML comment with an interpolated expression within an ng-repeat block. However, when I try to do this, the expression is not interpolated. For example:
<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元素标签),我只看到uninterpolated字符串({{item.id}}),而不是插入值。
When I view the DOM (i.e. the Elements tab in Chrom DevTools), I just see the uninterpolated string ("{{item.id}}") instead of the interpolated value.
什么是正确的语法在这里?
What's the correct syntax here?
推荐答案
这是这样矫枉过正,因为你可以只使用的意见建议显示:无
或相似的,但只是作为一个有趣的练习:
This is way overkill, since you could just use the suggestions in the comments of display: none
or similar, but just as a fun exercise:
要调用的注释指令的语法是:
The syntax to invoke a directive on a comment is:
<!-- directive: foo expression -->
我希望像 NG-绑定
支持意见,但事实并非如此。但是你可以很容易地创建自己的:
I was hoping that something like ng-bind
supported comments, but it doesn't. But you could easily create your own:
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}}" -->
这篇关于我怎么可以插入一个插值前pression HTML注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!