1

I am developing an Ionic apps.Here below is the code of my controller:

.controller('AreaListCtrl', ['$scope', '$location', '$sce', '$state', 'JsonResponseFactory', 'Loader',
function ($scope, $location, $sce, $state, JsonResponseFactory, Loader) {
    Loader.showLoading('Loading Area List ...');

    var data = [
                   { "Id": 0, "Name": "Gulsan", "ImageUrl": "http://admin-panel.studynovels.com/ContentImages/food_area_sample_1.jpg", "NumberOfRestaurants": 5 },
                   { "Id": 1, "Name": "Banani", "ImageUrl": "http://admin-panel.studynovels.com/ContentImages/food_area_sample_1.jpg", "NumberOfRestaurants": 4 },
                   { "Id": 2, "Name": "Dhanmondi", "ImageUrl": "http://admin-panel.studynovels.com/ContentImages/food_area_sample_1.jpg", "NumberOfRestaurants": 4 },
                   { "Id": 3, "Name": "Uttra", "ImageUrl": "http://admin-panel.studynovels.com/ContentImages/food_area_sample_1.jpg", "NumberOfRestaurants": 2 }
               ];

    for (var i = 0; i < data.length; i++) {
        data[i].ImageUrl = $sce.trustAsResourceUrl(data[i].ImageUrl);
    }

    $scope.userName = 'Ahmed All Razi';
    $scope.areaList = data;
    Loader.hideLoading();
}
])

And the code for my page is:

<ion-content>
    <div class="text-center" style="padding-left: 20px; padding-right: 20px;">
        <br />
        <h4 ng-show="userName">Hello, {{::userName}}</h4>
        <h6>Thank you for visiting our app. Please select your area from the list.</h6>
        <br />
    </div>

    <a class="card item" ng-repeat="area in areaList" ng-href="#/restaurantList/{{::area.Id}}">
        <h2 ng-show="area.Name">{{::area.Name}}</h2>
        <img ng-show="area.ImageUrl" ng-src="area.ImageUrl" />
        <hr />
        <h4 ng-show="area.NumberOfRestaurants">Total Restaurants {{::area.NumberOfRestaurants}}</h4>
    </a>
    <br />
</ion-content>

Every field is loading fine but image is not showing on page.

I am getting the follwing output Screenshot Here

[Note]:

  1. I have added the whitelist plugin cordova plugin add cordova-plugin-whitelist

  2. Added following line in my config.xml

  3. Added the following meta tag into my index.html

Can anyone help me to render the images properly on page.

3
  • 2
    Press F12 to go into developer tools, reload the page, watch the console for errors, also Inspect the <img> element and see if it points to the right URL in the src attribute. Commented Mar 10, 2016 at 6:44
  • I am getting an "404 not found" error for the images because the request has been sent to "localhost:8100/area.ImageUrl". How to load the image from the original url? Commented Mar 10, 2016 at 6:52
  • first test it by printing the value to html element then put it to src tag Commented Mar 10, 2016 at 9:22

2 Answers 2

2

You must use:

<img ng-show="area.ImageUrl" ng-src="{{area.ImageUrl}}" />

in your declaration (note the use of parenthesis, for more information read up on the ngSrc directive https://docs.angularjs.org/api/ng/directive/ngSrc).

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I tried this {{area.ImageUrl}} but it didn't work and in this case the blank thumbnail is not showing on my pages.
It should work, what does the src value say in inspecting the element now?
always use angular loop angularforEach loop with angular related stuff . while you are iterating objects angular for each is finer option than for loop .. it should load img as per GONeale answer docs.angularjs.org/api/ng/function/angular.forEach
1

Finally I found the solution. I added the following meta tag in my index.html which blocked the images to loading from external sources:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' mythicangel.com">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">

I just removed that line and image is loading now. Thanks everyone for responding.

3 Comments

Well, it also wouldn't have worked unless you specified {{area.ImageUrl}} so I would appreciate if you tick my answer as accepted :)
yes offcourse ... i forgot to mention ... thank you once again ... and I am sorry that I do not have enough point to give you a '+1' ...
Sorry I mean can you tick my answer, I don't think you get points for ticking your own anyway. But that's fine if you wish to leave this one as ticked.

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.