Begin loading your script file at the conclusion of the content load in AngularJS

I have implemented the script below:

$rootScope.$on('$includeContentLoaded', function (eve,uri) {
        var js = document.createElement("script");
        js.type = "text/javascript";
        js.src = '/assets/js/scripts.js';
        document.body.appendChild(js);
    });

However, the script is being appended multiple times based on how many times I use ng-include in my app. How can I ensure the script is included only after the entire page has loaded?

Answer №1

Always remember to check before adding the script to your code.

$rootScope.$on('$includeContentLoaded', function (eve, uri) {
    if(!document.querySelectorAll("script[src*='assets/js/scripts.js']").length){
        var js = document.createElement("script");
        js.type = "text/javascript";
        js.src = '/assets/js/scripts.js';
        document.body.appendChild(js);
    }
});

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

When the state of the grandparent component is updated, the React list element vanishes in the grandchild component. Caution: It is important for each child in a list to have a unique

In my development project, I've crafted a functional component that is part of the sidebar. This component consists of 3 unique elements. ProductFilters - serves as the primary list component, fetching potential data filters from the server and offer ...

Try using a function instead of JSON when setting options for the Angular-UI Bootstrap datepicker

Given the following code: <input type="text" class="form-control" uib-datepicker-popup="dd-MM-yyyy" ng-model="addNoPayRows[$index].date" is-open="addNoPayPopup[$index].opened" ng-required="true" close-text="Close" datepicker-options="datepickerOptions ...

Chrome runs requestAnimFrame at a smooth 60 frames per second, while Firefox struggles to keep up at

I recently developed a canvas animation using requestAnimFrame and saw great results in Chrome. However, when I viewed it in Firefox, it seemed like a slideshow was running instead of a smooth animation. I'm unsure what could be causing this issue. F ...

Updating entire DOM in javascript

Implementing undo-redo functionality in my project has proven to be quite complex, as every change affects numerous elements. I believe that saving and restoring the entire page may be the most effective solution. However, I have encountered issues with mi ...

What could be causing the malfunction of the map function, despite the code appearing to be correct?

I have encountered an issue in my React functional component where I am trying to display a list of notifications. The useEffect() function is being called to generate the "data" which should then be displayed on the page. The code seems to be running fine ...

The AngularJs Wizard Validation Inputfield is unable to locate the scope

Hello all, I am new to AngularJs and encountering an issue with my code. Currently, my HTML structure looks like this: <div class="wizardContainer"> <wizard on-finish="finishedWizard()"> <wz-step title="1"> <h1>Questio ...

Whenever an Ajax request is made to a PHP file, it consistently fails to retrieve any data despite the fact that the

Whenever I try to call a basic PHP page using AJAX, the response is always empty, even though the PHP code itself is functioning correctly. If you visit the URL of the PHP file directly, it echoes "Hello World" as expected. But when accessed from JavaScrip ...

Executing a function in a controller from a template only when a certain variable is true

Here is a row that I have: <a class="btn btn-default btn-xs" ng-click="list.showReview = list.showReview == $index ? -1 : $index; getValues(object.Id); "><i class=" glyphicon glyphicon-list-alt"></i></a> I am looking to only call ...

Incorporate Thymeleaf's HTML using the powerful JavaScript framework, jQuery

I am facing an issue where the Thymeleaf rendered HTML code is not being displayed by the Browser when I try to insert it using JavaScript. $('<span [[$ th:text#{some.property.key}]] id="counter" class="UI-modern"> </ ...

Exploring Object Array values with Javascript

I am working with an Object array and I need to implement a contains filter for every property. This means that the search should look for a specific keyword in any of the properties and return the object if it finds a match. Can you please provide guidanc ...

Issue with nextjs not returning the updated object correctly

I'm currently developing a nextjs app that incorporates authentication. There are two key functions that I execute on each page load. The first function checks for the existence of jwt cookies and then calls another function to validate the tokens if ...

Strategies for maintaining a sticky element while transitioning between containers

My challenge involves a sticky button located inside a container, but the sticky behavior only seems to work within another element that is full width (container-fluid). Is there any way to make the sticky behavior global? I have attempted using 'fix ...

Dealing with multiple select elements using jQuery when an option is chosen from each one

I am faced with a scenario where I have two select elements containing various options. My goal is to utilize jQuery to retrieve the values of these select elements only when valid options are selected. <div class="panel-heading"> <selec ...

Exploring the Differences Between Declaring and Not Declaring Controllers in AngularJS

Currently, I am working through the AngularJS tutorial found here: the AngularJS tutorial. My controller code is functioning properly and the page is loading correctly: var phonecatApp = angular.module('phonecatApp', []); phonecatApp.controller( ...

Data table created with functional components is not refreshing when columns are added or removed, which are stored in the state as an array of objects

I’ve been working on setting up a datatable with a checkbox dropdown feature to add or remove columns from the table. Everything is functioning properly, except for one issue – the table is not refreshing unless I click on one of the header titles. At ...

Any suggestions on resolving the "script timeout" issue while running a script using Python's SeleniumBase Library?

Recently starting to use Python, I am currently using Python's seleniumbase library to scrape a website and need to periodically run this fetch script. While experimenting, I encountered a script timeout error when the response time exceeded around 95 ...

Steer Your Way: How to Redirect to a Different Router or Middleware in Node.js and Express.js

I'm creating an application using VENoM stack, and I have set up some middleware in the API like this: const express = require('express'); const router = express.Router(); require('./routes/orderRoutes')(router); require('./ ...

Initiate an Ajax request from a hyperlink

Is it possible to trigger an Ajax request from a hyperlink? I am interested in updating the inner html of a div element upon clicking on a hyperlink through JavaScript. ...

What are the steps to manually render angular data tables?

Currently, I am utilizing angular-datatables with server-side processing. The datatable attribute needs to be specified in the table for it to be transformed into a datatable. However, my goal is to manually accomplish this after resolving an Ajax request. ...

Is your Node.js HTTP Request failing to function properly?

I am currently working on creating an HTTP Request function where all requests are directed to the same domain but with different file names. Unfortunately, I am encountering a problem where nothing is being displayed in the console and no data is being r ...