I want to replace certain letters (A,B,C,...) of a larger text with span
tags which i css into nice symbols. Since I later reuse these tags I created an angular template directive symbol
:
.directive('symbol', function() { return {
restrict: 'E',
replace: true,
template: '<span class="{{f1(kind)}}">{{f2(kind)}}</span>',
scope: { kind: '=' }
}})
I can replace the letters with span
s using a filter and regular expressions:
.filter('symbolify', ['$sce', function($sce) {
return function(text) {
text = text.replace(/[ABCD]/g, '<symbol kind="\'$&\'"></symbol>');
// this is the smallest example that reproduced the issue
return $sce.trustAsHtml(text);
}
}])
And insert it into the DOM with bind-html-compile:
<div bind-html-compile="text | symbolify"></div>
This does not work for me and I get $rootScope:infdig
errors.
Also, doing it this way, I can later not use ng-repeat
with a list of letters since I overwrite the scope in the directive:
<symbol ng-repeat="kind in ['A', 'B', 'C', 'D']"></symbol>
Does not work. (Solved: move repeat to enclosing div
and add kind="kind"
)
Here (updated) is a jsFiddle with the issue I described here (Opening the console might take some time).
Here (2. update) is a not so nice way of doing it, but it works.
Is there an official way of doing this that works for both cases? Or can I not use templates for this?