我想在 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 中的 Elements 选项卡)时,我只看到未插入的字符串 ("{{item.id}}") 而不是插入的值。
这里的正确语法是什么?
最佳答案
这太过分了,因为您可以使用 display: none
评论中的建议。或类似的,但只是作为一个有趣的练习:
在注释上调用指令的语法是:
<!-- directive: foo expression -->
我希望像
ng-bind
这样的东西支持评论,但事实并非如此。但是您可以轻松创建自己的: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}}" -->
关于angularjs - 如何插入带有内插表达式的 HTML 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245278/