A guide on how to efficiently preload images with the help of AngularJS and CreateJS

I am faced with the challenge of initializing an app where all media must load before the app can start. I am using AngularJS framework alongside CreateJS for preloading, but I am unsure of the correct approach. The code below is not functioning as intended. My goal is to display an animated .gif loading_circle_black.gif while all images and sounds are loading, then switch it out with the image file slide_4.png once everything has loaded.

Assume that the AngularJS controller is properly initialized and displays the initial animated gif. The question now is: How should I structure the HTML so that the image can be swapped successfully?

JavaScript

ControllerLibrary.prototype.MainStageController = function($scope) {
$scope.imageFile = "loading_circle_black.gif";    
$scope.status = "loading";

var queue = new createjs.LoadQueue();
queue.installPlugin(createjs.Sound);
queue.addEventListener("complete", handleComplete);
queue.loadFile({id: "sound_4", src: "/sounds/slide_4.mp3"});
queue.loadManifest([{id: "slide_4", src: "/img/slide_4.png"}]);

function handleComplete() {
    //createjs.Sound.play("sound_4");
    //var image = queue.getResult("/img/slide_4.png");
    $scope.imageFile = '/img/slide_4.png';
    $scope.status = "complete";
}

HTML

<body style="background-color: black;color: whitesmoke;">
    <div id="main" ng-controller="MainStageController" >
        <center>
            <!--<img id="loadingCircle" src="/img/loading_circle_black.gif"/>-->
            <img ng-src="/img/{{imageFile}}"/>
            <h2>{{status}}</h2>
        </center>
    </div>
</body>

Answer №1

It appears that the call to handleComplete is being made outside of the Angular context, so you will need to use $scope.$apply(). Give this a try:

function handleComplete() {
    $scope.$apply(function() {
       //createjs.Sound.play("sound_4");
       //var image = queue.getResult("/img/slide_4.png");
       $scope.imageFile = '/img/slide_4.png';
       $scope.status = "complete";
    });
}

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

Why is my "webpack" version "^5.70.0" having trouble processing jpg files?

Having trouble loading a jpg file on the Homepage of my app: import cad from './CAD/untitled.106.jpg' Encountering this error message repeatedly: assets by status 2 MiB [cached] 1 asset cached modules 2.41 MiB (javascript) 937 bytes (rjavascript ...

Tips for managing JavaScript within PHP code and vice versa

Usually, I include a lot of JavaScript on the website. However, the client informed me that they were unable to view the website correctly as they had disabled JavaScript on their end. After investigating, I discovered this issue. Now, my dilemma is wh ...

Having trouble with IE7 - unable to interact with elements below popup form

<script type="text/javascript">var switchTo5x=true;</script> <script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script> <script type="text/javascript">stLight.options({publisher:'b42661f5-2dc5 ...

The color of the three js cube is charcoal, definitely not a fiery red

Just dipping my toes into the world of three.js...my cube is displaying in black even though I set the color to red. Any ideas why? <!DOCTYPE html> <html> <head> <title>Experimenting with shapes</title> & ...

Navigating between pages in ReactJS using Material UI can be made simple and efficient with the

While working on creating a drawer for my dashboard using material-Ui, I encountered an issue with navigating between pages Let me start by sharing my app.js file where the product route is defined: <Provider store={store}> <BrowserRouter> ...

Updating input value using React state

Hey there, I'm currently using a color picker to update my this.state.colorPicked value, which is working well. Now, I want to create a function that will concatenate the state's colorPicked value with the input field when called. If the input fi ...

What could be the reason for JavaScript code successfully exporting to Excel using the old office extension .xls but encountering issues when trying to export

I am currently working on exporting an HTML table to Excel using JavaScript. I have encountered an issue where it does not export to the newer version of Excel with the xlsx extension. However, it works fine and exports to older versions of Excel with the ...

Animation for maximum height with transition from a set value to no maximum height

While experimenting with CSS-transitions, I encountered an unusual issue when adding a transition for max-height from a specific value (e.g. 14px) to none. Surprisingly, there is no animation at all; the hidden elements simply appear and disappear instant ...

Redirecting script upon successful connection detection

I have created a script that checks for internet connectivity using an image, and redirects if internet is available. However, the issue is that it caches the images, leading to attempts to load them even when offline. Is there a different approach I can ...

Exploring Heroes in Angular 2: Retrieving Object Information by Clicking on <li> Items

Currently, I am delving into the documentation for an angular 4 project called "Tour of Heroes" which can be found at https://angular.io/docs/ts/latest/tutorial/toh-pt2.html. <li *ngFor="let hero of heroes" (click)="onSelect(hero)">{{hero.name}}< ...

What is the best way to add an additional selected option after the page has been loaded?

I'm currently having trouble adding an extra option to a Chosen after the page has loaded completely. It works fine when I add values before the page loads, but once it's fully loaded, I can't seem to add values to the chosen component, as s ...

What is the best way to retrieve data from a jQuery AJAX call when it returns?

function isNewUsername(str){ var result; $.post('/api/isnewusername', {username:str}, function(data) { result = data.result; }, "json"); return result; } I am encounte ...

When is the appropriate time for ng-repeat and ng-href to be updated?

I am facing an issue where the value of an object in a list is changing, but this change is not being displayed correctly in ng-href. Here is the HTML code: <div class='tabrow docRow'> <span data-ng-repeat='f in dp.source.file ...

Replace the plus signs in a string with spaces using the JSON.stringify method

When utilizing AJAX, I am sending the string someString to a PHP handler. xmlHttp.open("POST", url, true); var someString = 'Foo+Bar'; var params = 'operation='+operation+'&json='+JSON.stringify(someString); xmlHttp.send( ...

Using Strapi to run basic raw SQL queries with MySQL

Trying to execute this query in my test but struggling with the correct syntax. return strapi.connections.default.raw(` delete from nurses; delete from users_permissions_user;`); }); Since this is not using PostgreSQL and MySQL ...

Unable to show the input's value

Need help in taking user input to display calculated values //html <div class="empty"> <h5> Enter Empty Seats </h5> <ion-item> <ion-input placeholder="Enter Number of Empties.." type="number" name="emptySeats" [( ...

Adding a .PHP File to Two Separate DIVs

I am using wordpress to create a custom theme. I'm interested in placing all the content from my slider.php file inside a div box on my index.php page. How would I go about doing this? SLIDER.PHP <div class="slider"> // All the image tags wit ...

Unable to retrieve the object property despite its presence

Within my React component, I have implemented a feature that retrieves a user's written story from the backend. The JSON data is stored in a variable called storyState. Despite seeing all the necessary properties like "title" when logging the data, I ...

"Encountering a duplicate identifier export error when attempting to launch my Angular2 project using

After initiating my Angular2 Project with the npm start command, I encountered an error stating "Duplicate identifier export." Despite extensive research, I have been unable to find a precise solution to this issue. Below, you will find the package.json, t ...

I am consistently encountering the error message: "Error: Unable to locate module './framer'"

I've been running into the same issue repeatedly. I'm in the process of creating a website for a barbershop and I'm attempting to integrate events into a Google calendar using the Google API. I've installed googleapis and framer, but I ...