मैं कोणीय 1.x में यह किया है, लेकिन मैं पता है कि कैसे एक कीवर्डकोणीय 2 में अनुच्छेद में किसी शब्द को हाइलाइट करने के लिए कैसे करें?
<td><div highlight="var" keywords="somename"> {{paragraph}}</div></td>
का उपयोग कर कोणीय 2..in कोणीय 1.x iam में एक गतिशील शब्द को हाइलाइट करना चाहते
मैं द्वारा ऊपर एचटीएमएल किया है नीचे कोणीय-light.js का उपयोग कर।
angular.module('angular-highlight', [])
.directive('highlight', function()
{
var component = function(scope, element, attrs) {
if (!attrs.highlightClass) {
attrs.highlightClass = 'angular-highlight';
}
var replacer = function(match, item) {
return '<span class="'+attrs.highlightClass+'">'+match+'</span>';
}
var tokenize = function(keywords) {
keywords = keywords.replace(new RegExp(',$','g'), '').split(',');
var i;
var l = keywords.length;
for (i=0;i<l;i++) {
keywords[i] = '\\b'+keywords[i].replace(new RegExp('^ | $','g'), '')+'\\b';
}
return keywords;
}
scope.$watch('keywords', function() {
//console.log("scope.keywords",scope.keywords);
if (!scope.keywords || scope.keywords == '') {
element.html(scope.highlight);
return false;
}
var tokenized = tokenize(scope.keywords);
var regex = new RegExp(tokenized.join('|'), 'gmi');
//console.log("regex",regex);
// Find the words
var html = scope.highlight.replace(regex, replacer);
element.html(html);
});
}
return {
link: component,
replace: false,
scope: {
highlight: '=',
keywords: '='
}
};
});
क्या यह कोणीय 2 + द्वारा सीधे 'आंतरिक HTML' में हेरफेर करने के लिए अनुशंसित है? –
सीधे 'रेंडरर' के माध्यम से नहीं – Maxime