Using Angular to dynamically load images through ajax requests

Is it possible to load an image via ajax, meaning to get the contents of the image via ajax and display it as an actual image? I understand how to handle the second part but

  1. how do you store the ajax data/blob in a variable?
  2. how can you initiate an ajax call in a directive?

I'm looking to access the real content of the image rather than just the src/url. This is needed in order to bypass the CSP restriction of Chrome apps.

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

Answer №1

Revision: Utilizing base64 to directly load an image

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

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

For updating the URL

You must connect the ng-src to a specific variable. Upon receiving an AJAX response, adjust 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/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide to examining pixels in Three.js

I have a query about comparing two textures at a pixel level in three.js. I am unsure of how to achieve this as the documentation for Three.js does not provide clear answers, with some classes remaining undocumented. In summary, I am looking to determine ...

Prevent the page from automatically scrolling back to the top when a user clicks on a "read more"

When implementing the function below on an HTML page to show/hide more details about a comment, there is an issue where the page scrolls up to the top whenever the "read more" link is clicked. This can be frustrating as it takes the user away from the comm ...

Exploring the Power of Jest and Vue Test Utils for Unit Testing in VueJS

Having recently started Unit Testing with vue, I am currently working on unit testing a navigation vue component. My initial goal was to test a method that simply sets a boolean value to false upon clicking. Utilizing vuetify, I attempted to replicate a bu ...

Is it possible for Excel to automatically populate specific fields with data obtained from a website using variables?

Currently, our ecommerce platform does not generate invoices. Instead, I manually transfer about 8 data points from the platform to an excel template for creating invoices. This process involves copy-pasting the required information before printing it out. ...

Guide on utilizing the disable feature in SortableJS for a Vue project

I have successfully implemented the draggable effect on my el table using element-ui-el-table-draggable and it's working perfectly. Now, I am looking to incorporate the disable option from SortableJS, but I'm unsure how to integrate these two fu ...

Scroll to the end of the main div's horizontal position instead of using offset().left

I'm struggling with an issue in my code. <script type="text/javascript"> $(function() { $('ul.nav a').bind('click',function(event){ var $anchor = $(this); /* ...

Efficiently convert a JSON array into an HTML table using the Jquery

Currently, I'm facing a challenge in my project. The issue arises when a query is sent to the server using jQuery, and an array is returned as a response. My struggle lies in transferring this data to a dynamic table since the column count varies with ...

Retrieving the current location synchronously via navigator.geolocation.getCurrentPosition();

I am having an issue with a function that is supposed to return the latitude and longitude as a string when called. Despite providing the correct values for latitude and longitude when alerting, it returns undefined. I would greatly appreciate any assist ...

The redirection to the HTML page happens too soon, causing the ASYNC functions to fail in saving the user's image and data to the server

Hey everyone! I'm facing an issue with async/await functions. Here's the code snippet from my backend where I'm trying to save details of a newly registered user. I'm puzzled as to why the Redirect() function is executing before the f ...

How can I use JavaScript or CSS to identify the specific line on which certain words appear in the client's browser?

Imagine I have the following HTML structure: <div> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco labor ...

No content appearing on React screen

I initialized a fresh react project using "npx create-react-app name". After removing unnecessary files, the application no longer displays anything. I've encountered issues with react-router and defining routes as well. index.html: <!DOCTYPE html ...

Simulated script in a different component

I am looking to simulate the functionality of Amazon AWS S3 getObject The specific code I aim to test is located in helper.js var AWS = require('aws-sdk'); var s3 = new AWS.S3(); exports.get_data_and_callback = function(callback, extra){ s3. ...

What are the steps for configuring a dynamic variable?

One issue I encountered was setting up a variable in vue that updates when the screen orientation changes. Despite adding this variable to the data object, it did not become reactive as expected. This is my initial data function for the component: data() ...

The issue of AngularJS dropdown selection not functioning properly in conjunction with ASP.NET MVC has been encountered

Hey there! I'm pretty new to angularjs with asp.net mvc and I've come across an issue with a drop down selection. Whenever I try to select an option from the dropdown, it's not working properly. Here's the HTML code: <select class ...

Icon fonts are not compatible with AngularJS, causing difficulties in their usage together

Incorporating icon fonts from Icomoon in my AngularJS framework project has been a challenge. Due to Strict Contextual Escaping (SCE), I am unable to properly generate these icons, resulting in the display of HTML number codes like "&#xe006;" instead o ...

Embracing the node mindset with a Java foundation

As a Java Developer, I have become accustomed to working in a sequential manner where tasks are executed one after the other or concurrently with multiple threads. The organization of code in a sequential way seemed logical under this paradigm. However, wh ...

What is the best way to specifically install the angular-ui-router.js file without the whole npm source code solution?

After installing the angular package using npm, I found the necessary JS files in the angular root directory. $npm install angular However, when installing the angular-ui-router package, I received the entire source code solution along with the required ...

Can Angular Universal SSR be activated specifically for Googlebot User Agents?

I am aiming to activate Angular Universal SSR only when the User Agent is identified as Googlebot. However, I am uncertain about how to instruct Angular Universal SSR to deliver server side rendered HTML exclusively if the User Agent is Googlebot. server ...

Converting information from a model into individual variables

I'm a newcomer to typescript and angular, and I've been attempting to retrieve data from firebase using angularfire2. I want to assign this data to variables for use in other functions later on. I am accustomed to accessing object members using d ...

Creating a transcluding element directive in AngularJS that retains attribute directives and allows for the addition of new ones

I've been grappling with this problem for the past two days. It seems like it should have a simpler solution. Issue Description The objective is to develop a directive that can be used in the following manner: <my-directive ng-something="somethi ...