1

I'm new on Angularjs. I making some test and I couldn't find out if is there way to do something like this:

<li ng-repeat="article in articles" class="thumbnail">
    <img  ng-src="{{encodeURI(article.image)}}"></a>
</li>

The idea is to manipulate the string with a native JS function.

1

1 Answer 1

6

You dont have to use the interpolate directive in these scenario's. You can use something that is much more understandable , like a function.

<li ng-repeat="article in articles" class="thumbnail">
<img ng-src="encode(article.image)">
</ii>

Now encode should be a function in either the scope that contains the articles or in the inner scope (Note : ng-repeat creates a new scope for each item it creates. So in this example for each article there will be a new scope ).

Lets say your controller is called ArticleCtrl ( I am going to assume )

function ArticlesCtrl($scope){
    $scope.articles = [];
    $scope.encode = function(url){
        return encodeURI(url);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, is a good solution. I have created a custom filter to resolve it, so now I do something like this: <img ng-src="{{article.img | encodeURI}}"> What do you think is the best approach?
To me it looks like the filter is a better approach. Mostly because that is write one fire many times approach and I am assuming you would have to encode uri in more than 1 controller. If you do my approach you would have to write this in all the controllers which need that functionality. A filter seems appropriate unless someone else has an opinion as to why that wont fit in this case or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.