Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute code at startup of angular application

Tags:

angularjs

Is there a way I can execute some JavaScript code at start-up of my AngularJS application? I have some common code that I need to make sure runs before any of the app directives/controllers. I do not want to be tied to routes and ng-view, I need this to be a generic solution for any ng-app.

I thought I could use Module Config, and I actually tried that, but I am trying to call a service, which seems impossible to access on Module Load.

like image 539
Michel Habib Avatar asked Oct 09 '13 15:10

Michel Habib


People also ask

Which file is executed first in Angular?

ts” file, i.e., “main. ts” file is the main file from where the execution of an Angular application will start.

What is the starting point of Angular application?

The angular. module is the entry point of Angular applications. Each application has just one module that gets the rootElement <html> or <body> tag.

What is App_initializer in Angular?

What is APP_INITIALIZER. The APP_INITIALIZER is an instance of InjectionToken . It is a built in Injection token provided by Angular. The Angular will execute the function provided by this token when the application loads. If the function returns the promise, then the angular will wait until the promise is resolved.

What is Bootstraping in Angular?

bootstrap is a function component in the core ng module that is used for starting up the Angular application manually, which gives you more control over how you initialize your application. The syntax for angular.


2 Answers

You can do like this,

var app = angular.module('myApp',[]);  app.run(function($rootScope) {     //..... }); 
like image 123
Anshad Vattapoyil Avatar answered Oct 06 '22 00:10

Anshad Vattapoyil


Short version

You need to use the module.run(initializationFn) function, where the actual method can depend on services. You can inject dependencies as per usual:

var app = angular     .module('demoApp', [])     .run(['$rootScope', function($rootScope) {         $rootScope.bodyClass = 'loading';         // Etc. Initialize here.     }]); 

The example has initialization dependent on $rootScope, but you can also inject services, etc.

Longer version

The relevant module.run documentation is rather terse, as are the other (excellent) answers. Let me combine it into a more verbose example, that also shows how to inject a factory created service in your initializationFn:

var app = angular.module('demoApp', []);    // Service that we'll also use in the .run method  app.factory('myService', [function() {    var service = { currentItem: { started: new Date() } };        service.restart = function() {      service.currentItem.started = new Date();    };        return service;  }]);    // For demo purposes  app.controller('demoCtrl', ['$scope', 'myService', function($scope, myService) {    $scope.header = 'Demo!';    $scope.item = myService.currentItem;    $scope.restart = myService.restart;  }]);    // This is where your initialization code goes, which  // may depend on services declared on the module.  app.run(['$window', 'myService', function($window, myService) {    myService.restart();    $window.alert('Started!');  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>    <div ng-app="demoApp" ng-controller='demoCtrl' ng-cloak>    <h1>{{ header }}</h1>    <p>Current item started: {{ item.started }}</p>    <p><button ng-click="restart()">Restart</button></p>  </div>
like image 29
Jeroen Avatar answered Oct 06 '22 01:10

Jeroen