user2429082 user2429082 - 4 months ago 10
HTML Question

Display images through html img tag with angularjs and ionic

I'm currently doing an hybrid mobile app with ionic and Angularjs and i'm trying to display images through an img html tag. My page is composed of a caroussel at the top (which works well, it displays images) and a list with small images.
In the controller of my page there is :

app.controller('SalleCtrl', function ($scope,$location) {

$scope.salle = {
"num": "2",
"imgR": [
"img/art_affiche_6_thumb-300x300.jpg",
"img/art_affiche_6_thumb-300x300.jpg"
],
"title": "Salle 2 : Premières peintures",
"_audio_guide": [],
"_audio_guide_fe": [],
"_audio_guide_en": [],
"artworks": [
{
"img": "img/art_affiche_6_thumb-300x300.jpg",
"href": "http://172.16.12.158/wordpress/mcm_oeuvre/oeuvre-14-2/",
"title": "Oeuvre 14"
},
{
"img": "img/art_affiche_9_thumb-300x300.jpg",
"href": "http://172.16.12.158/wordpress/mcm_oeuvre/oeuvre-3/",
"title": "Oeuvre 3"
}
]
};
});


And in my html page there is :

<ion-view title="{{salle.title}}">

<ion-nav-buttons side="right">
<button menu-toggle="right" class="button button-icon icon ion-navicon"></button>
</ion-nav-buttons>
<ion-content class="has-header" id="" >
<ul rn-carousel class="image">
<li ng-repeat="image in salle.imgR" style="background-image:url({{ image }});background-color:black;background-repeat:no-repeat;background-position:center center;background-size : contain;">
</li>
</ul>

<div class="list">
<a ng-repeat="artwork in salle.artworks" class="item item-avatar" href="{{artwork.href}}">
<img ng-src="{{artwork.img}}">
<h2>{{artwork.title}} {{artwork.img}}</h2>
</a>
</div>

</ion-content>




When I display it on a browser everything works well. But when I try on my smartphone the caroussel works, it displays images, but the list doesn't show the images, unstead of {{artwork.img}} show me all the images.
I try to :


  1. replace 'ng-src' by 'src' but nothing happens

  2. replace ng-src="{{artwork.img}}" by ng-src="img/art_affiche_6_thumb-300x300.jpg" it works.



Apparently the binding is not correctly made... Have you any idea why? And how resolve it?!
Moreover on my smartphone, the path of the images looks like "cdvfile://localhost/persistent/...". The caroussel works well, but the list of image doesn't works, and when I translate "cdvfile://localhost/persistent/..." to "file://mnt/sdcard/..." it works. Why?!

Answer

I finally find the answer. The problem is due to the fact that angularjs prevent XSS attacks via html link with the function imgSrcSanitizationWhitelist. So the image path which begin with 'cdvfile://localhost/persistent' is prefix by "unsafe:" and so the image is not display. In order to get around this problem I had to override this method, to do that in my config of my main module I add this code :

var app = angular.module( 'myApp', [] )
     .config( ['$compileProvider',function( $compileProvider ){ 
         $compileProvider.imgSrcSanitizationWhitelist(/^\s(https|file|blob|cdvfile):|data:image\//);
       }
     ]);

The discussion with the answer