I'm trying to replace some words (all the words present in my rootscope data array) for add a tooltip.
My code :
.directive('replaceDirective', function($rootScope, $compile) {
return {
restrict: 'A',
replace: true,
link: function(scope, element, attrs) {
//For exemple
//$rootScope.data = ["word1", "word2", "word3", etc..];
var test = {
getTest: function(e, word) {
return RegExp(e.toString().replace(/\bxx\b/g, "").replace(/xx/g, word), "g")
}
};
scope.$watch(attrs.replaceDirective, function(html) {
for (var i=0; i<$rootScope.data.length; i++) {
var tooltipWord = $rootScope.data[i].word;
var tooltipDescription = $rootScope.data[i].def;
var template = '<a data-html="true" class="tooltip" title="' + tooltipDescription + '" data-toggle="tooltip" data-placement="top" tooltip>' + tooltipWord + '</a>';
var oldRegex = /\bxx\b/;
html = html.replace(test.getTest(oldRegex, tooltipWord), template);
}
element.html(html);
$compile(element.contents())(scope);
});
}
};
});
It doesn't work! I have an error.
The error is:
"Error: nothing to repeat
.link/test.getTest@file:www/js/directives.js:336:28
.link/<@file:www/js/directives.js:357:76"