$(document).ready alternative for AngularJS

17,385

Solution 1

Many jQuery plugins depend on a workflow of 1. draw the DOM. 2. run an init() function to set up code against those DOM elements.

That workflow fares poorly in Angular, because the DOM isn't static: Angular sets up and destroys DOM nodes on its own lifecycle, which can overwrite event bindings or DOM changes made outside Angular. Document ready isn't particularly useful when you're using Angular, because all it indicates is that Angular itself is ready to start running.

To use Angular effectively, you have to get into the habit of initing code only when it's actually needed. So instead of a big bucket of init_foo(); init_bar(); on document.ready, you should have a Foo directive with its own init code, and a Bar directive with its own specific init code, and so on. Each of those directives should only modify the DOM created by that specific directive. This is the only safe way to ensure that the DOM elements you need to modify actually exist, and that you're not creating conflicts or unexpected interdependencies between directives.

To take one example: I'm guessing your init_flot_chart() crawls down through the DOM looking for a particular element inside of which it'll draw a flot chart. Instead of that top-down approach, create a directive:

angular.module('yourApp')
  .directive('flotWrapper', function () {
    return {
      template: "<div></div>",
      scope: {
        data: '@'
      },
      link: function(scope, elem, attrs) {
        var options = {}; // or could pass this in as an attribute if desired
        $.plot(elem, scope.data, options); // <-- this calls flot on the directive's element; no DOM crawling necessary
      }
    };
});

which you use like this:

<flot-wrapper data="{{theChartData}}"></flot-wrapper>

...where theChartData is an object containing whatever data is to be drawn in the chart. (You can add other attributes to pass in whatever other parameters you like, such as the flot options, a title, etc.)

When Angular draws that flotWrapper directive, it first creates the DOM element(s) in the directive template, and then runs whatever is in its link function against the template's root element. (The flot library itself can be included via a plain old <script> tag so its plot function is available when the directive needs it.)

(Note that this wouldn't update automatically if the contents of theChartData change; a more elaborate example which also watches for changes and responds appropriately can be seen here.)

Solution 2

As you are using ngRoute, your controller will run when the page is loaded. You can call an init() method when the controller starts to do whatever you want.

function MyCtrl($scope) {
    function init() {
        console.log('controller started!');
    }
    init();
}

As a side note, it is a best practice recommanded in John Papa Angular's guide.


Another possibily is to use the ng-init directive, for example:

<div ng-controller="MyCtrl" ng-init="myFunction()">...</div>
Share:
17,385
Jérémy
Author by

Jérémy

Updated on June 28, 2022

Comments

  • Jérémy
    Jérémy almost 2 years

    I'm using a template called Gentelella and I'm trying to implement AngularJS into it. However, I'm having an issue with a certain Javascript file. In the end of this file, a $(document).ready function is called which initialises Javascript code that makes some changes in the HTML code. The issue is that the $(document).ready function is called too early, before the HTML is fully loaded.

    This issue occurs probably because I'm using ngRoute and this injects the template html file into the ng-view of the index.html. When this happens, the DOM probably already announces a document ready before AngularJS has injected the template (=HTML).

    So basically, I just need to find a way to call some code in a Javascript file once AngularJS has injected the template.

    I attached some code to gain some insight into the issue:

    Snippet of the custom.min.js

    $(document).ready(function () {
      init_sparklines(), init_flot_chart(), init_sidebar(), init_wysiwyg(), init_InputMask(), ...
    });
    

    Snippet of the main.js:

    .config(function($routeProvider, $httpProvider) {
    
      $routeProvider.when('/', {
        templateUrl : 'dash.html',
        controller : 'dash',
        controllerAs: 'controller'
      }).when('/login', {
        templateUrl : 'login.html',
        controller : 'navigation',
        controllerAs: 'controller'
      }).when('/plain_page', {
        templateUrl : 'plain_page.html',
        controller : 'dash',
        controllerAs: 'controller'
      }).otherwise('/');
    
      $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    
    })
    

    Thanks in advance!

  • Jérémy
    Jérémy about 7 years
    thank you for your answer. This solution doesn't seem to work. I called an alert inside the init() method and when the alert was called, the HTML wasn't fully loaded yet.
  • Harshit Jain
    Harshit Jain about 7 years
    $timeout is not a full proof solution as we cannot decide the after which the function needs to be called.
  • Jérémy
    Jérémy about 7 years
    Thank you for your answer. This one doesn't work. The function is already called before the actual HTML is injected.
  • Jérémy
    Jérémy about 7 years
    This one doesn't work at all. Do I need to fire the viewContentLoaded event somewhere?
  • brasofilo
    brasofilo about 7 years
    Would be better if you explained why and how your code solves the problem...