Angular Change image src depending on class

2019-09-10 05:12发布

If image has 'loaded' class, i need to change image src to "{{item.preview2}}"

<div ng-repeat="item in items">
<img src="{{item.preview}}" class="grid-img" />
</div>

2条回答
女痞
2楼-- · 2019-09-10 05:40

In the case you describe I would just use ng-if to control what elements are rendered to the DOM. for example:

<img ng-if="item.loaded" src={{item.preview2}} class="grid-img-loaded"/>
<img ng-if="!item.loaded" src={{item.preview}} class="grid-img"/>

Hope this helps.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-10 05:44
directive('loaded', function(){
    return {
        restrict: 'C',
        link: function(scope){
             scope.item.preview = scope.item.preview2
        }

    }

})

and in html

<img ng-src="{{item.preview}}" class="grid-img" />
查看更多
登录 后发表回答