javascript - Initialize Angular Service (factory) when application start -
in angular application adding tracing functionality, should work separate plugin, if script included html angular should create service , initialize (run) it. plugin service because has mandatory dependency on $rootscope. implementation select use angular factory, (code in myservice.js):
angular.module('app').factory('mysevice', ['$rootscope', '$interval', servicefunc]); function servicefunc($rootscope, $interval) { return { run: function () { ... } } } and have problem how initialize it. solution enhance angular app run function (code in myservice.js):
window.app.run(['mysevice', function (srvc) { srvc.run(); }]); where app defined in separate main app.js file like:
var app = window.app = angular.module('app', [...]); app.run(['$state', 'breeze', ..., function ($state, breeze, ...) { ..real initialization.. } the code working fine. both main run , run myservice calls fine. application service working well, code looks ugly, storing app in global window object , multicasting run function.
is there better way angular service , initialize after app starts other angular services dependencies.
you can call angular.module('app').run(function(){...}). not need global variable anymore.
example:
angular.module('app').run(['mysevice', srvc.run.bind(srvc)]);
Comments
Post a Comment