Using Angular: connecting $viewContentLoaded to manually initiate app bootstrapping

I have an Angular module that I am bootstrapping manually and attempting to access its $rootScope to add an event listener for $viewContentLoaded.

Below is the code snippet:

angular.bootstrap(el, [appname]);
//////////////////////////// Fixing links
console.log('patching');
var $rootScope = angular.injector([appname]).get('$rootScope');
console.log($rootScope);// Scope { $id=139,  $root=Scope,  $$destroyed=false,  more...}
$rootScope.$on('$viewContentLoaded', function () {
    console.log('$viewContentLoaded');
});

The issue is that I'm receiving a $rootScope with id 139, but the event is not firing as expected.

If I try to access the $rootScope inside the main controller of my module, I get:

console.log('myController',$rootScope);
Scope { $id=120,  $$childTail=Scope,  $$childHead=Scope,  more...}

Here, I'm getting a $rootScope with id 120.

When I attach the event handler to the controller within the module, it functions correctly.

So, what am I doing incorrectly? How can I retrieve the correct $rootScope of a module outside of its controller?

It's important to note that I am working in a multi-app environment. I have a main module that loads and bootstraps other modules, and I want to detect when the view content of these other modules has finished loading without individually adding code to each one manually...

Note: I am able to access the $rootScope using this method:

var $rootScope = angular.element(el).scope().$root;

However, this approach does not work when

$compileProvider.debugInfoEnabled(false);
is enabled :(

Answer №1

Alright, here's the code I came up with for anyone interested:

(function (angular) {

    var originalMethod = angular.module;

    angular.module = function (name, requirements, configFunction) {
        var module = originalMethod(name, requirements, configFunction);

        module.run(function ($rootScope) {
            //////////////////////////// Fixing links
            $rootScope.$on('$viewContentLoaded', function () {
                ___fixLinks($('[ng-view],ng-view,.ng-view','[ngg-app="' + name + '"]'));
            });
        });

        return module;
    };

})(angular);

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

Javascript datatables do not allow for setting a default column sort

I am encountering an issue where I need to sort the results by a specific column on page load. In this case, I want the initial results to be displayed in descending order based on "RecordDate". However, it seems that the server side is blocking any sort ...

retrieval: unspecified information obtained through body parsing (Node.js Express)

Having just started working with React.js and Node.js, I encountered a simple issue that I can't seem to solve. I am using the lightweight fetch API to post data and trying to receive that data using body-parser, but it always returns undefined. impo ...

Instructions on opening a modal and changing the source of the iframe within the modal

I currently have a modal within my application that is triggered by Bootstrap: <div class="modal full-page fade" tabindex="-1" role="dialog" id="fullpageModal"> <div class="full-page-content"> <button type="button" class="close" d ...

Encountered a problem initializing Rangy.js on Internet Explorer

Currently, I am developing an angular application that utilizes textAngular along with rangy-core and rangy-selectionsaverestore. However, I am encountering some errors specifically on the latest version of Internet Explorer: Module 'WrappedSelection ...

Menu Options in Material UI Navbar

I am currently working on incorporating an icon in my navbar that, when clicked, will reveal a dropdown list of notifications. Although I have come across several code examples for dropdown menus, none of them have completely assisted me or provided specif ...

Leveraging geoPosition.js in conjunction with colobox

How can I create a colorbox link that prompts the user for permission to access their location, and if granted, displays a map with directions from their current location? I've managed to get it partially working, but there's an issue when the us ...

Encountering the "ERR_FILE_NOT_FOUND" message within the popup of my Chrome extension

Currently, my manifest file is structured as follows: { "manifest_version": 2, "name": "My Extension", "description": "A starting point for creating a functional Chrome extension", "version": "0.0.1", "browser_action": { "default_popup": " ...

Exploring error management in Vue3 and Vite when deploying to production

After following the error handler setup in the Vue documentation, I encountered a difference between using it on the development server and in production. The error handler effectively pinpointed the component and line number where the error occurred durin ...

Is it possible to locate and eliminate the apostrophe along with the preceding letter?

My objective is to tidy up a character string by removing elements that are not essential for the user and SEO, specifically the (letter before the apostrophes) in this case. I am looking for a regex solution or explanation of how to achieve this in PHP, a ...

What is the best way to transfer information from the window method to the data function in a Vue.js application?

Is there a way to transfer information from the window method to the data function in a vuejs component? Take a look at my window method: window.authenticate = function(pid, receiptKey) { console.log("Authentication"); console.log(this) localStorag ...

The Vue2 @click event does not function properly when using v-html within a different component

I currently have a sign up form that includes an Alert component for displaying any errors. One of the conditions may prompt a message saying "You already have an account, click here to log in". The error messages are passed as props to the "Alert" compon ...

Recurring Triggering of Angular Directive

As a newcomer to AngularJS, I am facing an issue with my custom directive called sizeWatcher. This directive, when placed as an attribute in the HTML, is supposed to retrieve the height of the element it's attached to and store that height in a scope ...

Vue is set up to monitor changes in two connected input fields for user input exclusively

Two input fields are available, where the value of one can affect the other. If a value between 1 and 200 is entered in the level field, Vue will look up the corresponding points for that level and populate the points field with them. Conversely, if a us ...

Guide to dynamically implementing pagination through AJAX calls

In this javascript code snippet, I have a class named PurchaseHistory. var baseUrl = null; var parameters = null; var currentPageNumber = null; var TotalPages = null; var PageSize = null; $(document).ready(function () { baseUrl = "http://localhost/AP ...

What is the process for creating custom event bindings in AngularJS?

There is a custom event called core-transitionend (specifically triggered by Polymer), and I am able to specify an event handler using document.addEventListener(). However, what would be the most recommended approach for achieving this in AngularJS? Alter ...

Trouble with X-editable linking to database for updates

Utilizing the X-Editable plugin within my PHP application to update fields in a table and utilizing a POST file to update the database. Below is the form code: <table id="restaurant" class="table table-bordered table-striped"> <tbody> ...

I'm a complete programming newbie and I want to start learning JavaScript, jQuery, and other programming languages. Where should I

Coming from a design background with zero programming knowledge, I have recently learned XHTML and CSS. Now, I am eager to expand my skills by mastering JavaScript, jQuery, and more. Where should I begin? This will be my first foray into programming. Whil ...

Which framework should be used: client-side or server-side?

I am working on a project similar to craiglist, where users can post announcements for everyday items like cars and flats. I have already developed a significant portion of the backend using a RESTful API in three-tier architecture with Java, connecting to ...

Reconfigure the API to segment its components into individual variables

I'm currently working with an API that offers a wide range of available calls. In my VUE code, I am looking to modify it so that depending on which button is clicked, a different call is triggered. You can check out one example of this here: <>&am ...

In JavaScript, the function is unable to access elements within an iteration of ng-repeat

I am using ng-repeat to display datepickers that are powered by flatpickr. To make this work, a script needs to be added on the page for each input element like so: <script> $('[name="DOB"]').flatpickr({ enableTime: false, dateForm ...