I am new to Angularjs and are trying to create a dictionary directive that searches and replaces texts with anchor tags. The search and replace part works fine but i can't get the ng-click to work.
HTML
<div mk-dct>Lorem ipsum [dolor] sit [amet]</div>
Angular
app.directive('mkDct', function () {
var pattern = /\[([^\]]+)\]/g;
return {
restrict: 'A',
link: function(scope, element, attrs) {
var txt = element.html();
var m = txt.match(pattern);
for(var i = 0; i < m.length; i++){
var word = m[i].substring(1, m[i].length-1);
txt = txt.replace(m[i], '<a class="dict-item" ng-click="func(\''+word+'\')" href="#">'+ word + '</a>');
}
element.html(txt);
}
};
UPDATE
The commented lines below made it work as expected.
app.directive('mkDct', function ($compile) {
var pattern = /\[([^\]]+)\]/g;
return {
restrict: 'A',
scope: true,
link: function(scope, element, attrs) {
var txt = element.html();
var m = txt.match(pattern);
for(var i = 0; i < m.length; i++){
var word = m[i].substring(1, m[i].length-1);
txt = txt.replace(m[i], '<a class="dict-item" ng-click="func(\''+word+'\')" href="#">'+ word + '</a>');
}
// Compile to template.
txt = $compile(txt)(scope);
// Clear and append.
element.empty().append(txt);
}
};
});