1

I wish to load a image via ajax i.e get contents of image via ajax and show this as a image. I Know how to do the later part but

  1. how do i get the ajax data/blob into a variable
  2. how can i make a ajax call in a directive

I want the actual content of image not just src/url. I want to do this to overcome the CSP restriction of chrome apps

https://developer.chrome.com/apps/contentSecurityPolicy

6
  • I think that ajax call in directive is wrong Commented Feb 29, 2016 at 17:38
  • stackoverflow.com/questions/18460684/… Commented Feb 29, 2016 at 17:50
  • @Dave thanks for first part... how about second Commented Feb 29, 2016 at 17:53
  • @aWebDeveloper You can bind the directive to a button, bind a click event listener to the element (which is the button), and trigger the ajax call function using angular's $http service Commented Feb 29, 2016 at 17:57
  • why would you need to make ajax call in directive? Even if you do there is no difference in approach Commented Feb 29, 2016 at 17:58

1 Answer 1

1

Edit: To load image directly using base64

<img data-ng-src="data:image/png;base64,{{imageData}}" />

Updated Fiddle - http://jsfiddle.net/1koLs9fh/2/

To update URL

You need to bind the ng-src to some variable. On AJAX response update this variable.

    <div ng-app="myApp">
        <div ng-controller="myCtrl">                
            <img ng-src="http://{{url}}"/>
            <button ng-click="loadImages()">load images</button>
        </div>
    </div>

var myApp = angular.module('myApp', []);

myApp.controller('myCtrl',
    function ($scope) {        
        $scope.loadImages = function() {
        $scope.url = 'cdn1.www.st-hatena.com/users/ho/howdy39/profile.gif';       
        }
    }
);

http://jsfiddle.net/1koLs9fh/

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

7 Comments

never want to use src ... browser will make funky requests with uncompiled url which will of course be 404
agreed. Use ng-src. Just an example.
i don't want the src but actual image in src. i.e make ajax call and get the actual image not just src.
you mean actual bytes?
@aWebDeveloper need to transpose that image file to a base64 dataUri then. Doesn't really make sense sending image itself if it is stored web accessible directory
|

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.