Because my ng-bind-html uses ng-click, I need to use compile, but the documents I read all write compile in the directive. The problem is that I use compile in the filter. How to use it? Online No resources can be found at all.
app.filter('httpClickFilter',function($sce){
return function(inputText){
var textHttpArray = inputText.split('http');
textHttpArray[textHttpArray.length-1] = textHttpArray[textHttpArray.length-1] + ' ';
for(var i = 1;i < textHttpArray.length; i++){
var textSubstring = 'http' + textHttpArray[i].substring(0,textHttpArray[i].indexOf(' '));
var replaceTextHttp = "<span style='color:#fe7979' ng-click='httpLinkClick()'>"+textSubstring+"</span>";
inputText = inputText.replace(textSubstring,replaceTextHttp);
}
return $sce.trustAsHtml(inputText);
}
})
HTML:
The general meaning is: intercept a text field, find the http link inside, and modify the HTTP link to be clickable. It’s okay to use filters here!
http://stackoverflow.com/questions/19726179/how-to-make-ng-bind-html-compile-angularjs-codeSupplement
If you are just doing data conversion, using filter here is undoubtedly the right choice. But you are involved in this kind of dynamic event binding. At this time, it is not suitable to use filters to handle this demand. The filter performs additional processing on our input, targeting the data. Note that it targets the data. The responsibilities are very clear. If you use the filter to process the rendering logic, it violates the single responsibility principle. This is not the original intention of Angular's design of the filter.
OK, if you insist on writing it in the filter, then theng-click
method can only be hung on $rootScope, there is no other choice, just HACK to implement it
But unfortunately, it is useless, because trustAsHtml handles all the bound events.httpLinkClick
http://stackoverflow.com/a/18149450/2586541