genkio / blog

Stay hungry stay foolish
https://slashbit.github.io/blog/
0 stars 1 forks source link

Angular bootstrapping process #104

Open genkio opened 8 years ago

genkio commented 8 years ago
// manual bootstrap
window.onload = function() {
  // find the root element
  var $rootElement = angular.element(window.document);

  var modules = [
    'ng', 
    'myApp',
    // inline module to tell system where the root element is
    function($provide) {
      $provide.value('$rootElement', $rootElement);
    }
  ];
  // create injector, one injector per application
  var $injector = angular.$injector(modules);

  // from injector to get hold of compile service
  var $compile = $injector.get('compile');

  // compile service traverses the dom starting from the root element
  // compile service returns the link function
  // compositeLinkFn is the collection of all link functions in the system
  var compositeLinkFn = $compile($rootElement);

  // get hold of the root scope
  var $rootScope = $injector.get('$rootScope');
  // link function creates all the bindings, which leads the 'live view' be created
  compositeLinkFn($rootScope);

  // kick off
  $rootScope.$apply();
};