I wrote a filter that appears to be working fine, although I am getting the following error.
TypeError: Cannot read property 'replaceAll' of undefined
The filter replaces erroneous relative urls with absolute ones. Here is the filter and markup.
//located elsewhere
String.prototype.replaceAll = function(s,r){
return this.split(s).join(r);
}
//located elsewhere
var imgBase = '<img src="http://discourse.mysite.com';
app.filter('prependImg_topic', function(){
return function(cooked) {
return cooked.replaceAll('<img src="//discourse.mysite.com', imgBase);
}
})
Markup:
<div class="topic-post-body" ng-bind-html="topic.post_stream.posts[0].cooked | prependImg_topic "></div> "
I have an inclination that I need to use $sce
because I am inputting the data in ng-bind-html
. If I print the value like so: {{topic.post_stream.posts[0].cooked}}
, it exists, so I know it is not undefined. In addition, the filter works and properly writes urls. Go figure.
I can provide any additional details needed to help answer. Thanks.
EDIT: Another oddity that may help is that inside of a ng-repeat
this filter works, and throws no errors. Trippy stuff!
<div class="post" ng-repeat="post in posts" ng-if="$index > 0">
<div class="topic-post-body" ng-bind-html="post.cooked | prependImg_topic"></div>
</div>
I guess the data is loaded asynchronously, if that's the case it is no wonder the filter throws an errors since it is trying to operate on (yet) non-existent data. The check you make {{ topic ... }}
doesn't prove anything because it will print the data if that data will eventually come (better check with console.log
). The reason why the filter inside ng-repeat
doesn't throw any errors is because the ng-repeat
will only work if there is any data to iterate on, that means that when the filter inside ng-repeat
runs the data is already there. That's also the reason why you should always construct filters in such a way that they can handle undefined
or null
values without throwing.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments