最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Manipulate string inside {} in Angularjs - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jun 19, 2015 at 3:12 andy_314 asked Oct 7, 2012 at 4:37 andy_314andy_314 4645 silver badges18 bronze badges 1
  • looks like a good candidate for a filter as @ganaraj suggested: docs.angularjs/guide/… – Guillaume86 Commented Oct 8, 2012 at 15:48
Add a ment  | 

1 Answer 1

Reset to default 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);
    }
}
发布评论

评论列表(0)

  1. 暂无评论