我有一个小的角度过滤器,它插入一个(引导)图标来代替真实值。它可以工作,但html是经过编码的:
var conApp = angular.module('conApp', []);
conApp.filter('trueIcon', function($filter){
return function(booleanValue){
return booleanValue ? '<i class="icon-ok"></i>' : '';
}
});
我必须实现另一个过滤器来解码html吗?有没有一种"angular“的方式来做到这一点?
发布于 2013-02-05 01:47:50
您需要使用ng-bind-html来呈现html。
<span ng-bind-html="foo | trueIcon"></span>
也就是说..。这真的不是过滤器的作用。过滤器更多的是用于清理写入视图的数据,而不是生成视图/DOM本身的元素。为此创建一个指令可能会更好:
app.directive('trueIcon', function() {
return {
restrict: 'E',
template: '<i ng-class="{\'icon-ok\': value, \'icon-not-okay\': !value}"></i>',
scope: {
value: '='
}
};
});
<true-icon value="foo"></true-icon>
发布于 2021-03-04 18:41:32
我找到的另一种方法是将\r\n转换为
是使用一个小函数和*ngFor。
在ts文件中,添加函数
splitLines(text: String): string[] {
return text.split(/\r?\n/);
}
在html中
<span *ngFor="let row of splitLines(multilineStringVariable)">
{{row}}<br/>
</span>
https://stackoverflow.com/questions/14692354
复制相似问题