Is there a way to initialize an Angular Service without DI into controller

Is there a way to initialize an Angular Service without DI into controller

By : Александр Качковский
Date : November 21 2020, 07:31 AM
like below fixes the issue Because you instantiate BroadcastClock, I think you still should be able to handle it via a service by either exposing it or exposing functions that handles it.
Anyway you can run initializing blocks with .config(), at provider injection time, or in your case .run() at the beginning at application's run time:
code :
app.run(function($interval, $rootScope) {
  var clock = new BroadcastClock($interval, $rootScope);

Share : facebook icon twitter icon
Initialize a non injected angular service

Initialize a non injected angular service

By : spudgun
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Generally, Angular gives you the module.run(fn) API to do initializations. The fn argument is fully injectable, so if you have a service, e.g. myService, that exposes an init() method, you can initialize it as:
code :
angular.module(...).run(['myService', function(myService) {
angular.module(...).service(function() {
    ...initialization code...
angular.module(...).run(['myService', function() {
    // nothing else needed; the `myService` constructor, containing the
    // initialization code, will already have run at this point
angular.module(...).run(['myService', angular.noop]);
Why my angular controller does not initialize vm vars?

Why my angular controller does not initialize vm vars?

By : user3200916
Date : March 29 2020, 07:55 AM
I hope this helps . I'm using controller as syntax in my angular web app and from the controller I call a factory which does REST GET calls to a url with a var menuTitle path parameter: , If you do:
vm.planMenuGroup = getHeaderMenuGroup("plan");
code :
function getHeaderMenuGroup(controllerVar, menuTitle) {
     menuDataFactory.getHeaderMenu(menuTitle).then(function (response) {
           controllerVar = response.data;
vm = this;
// registered user menus
getHeaderMenuGroup(vm.planMenuGroup, "plan");
Ember cli, how to initialize a service with a controller?

Ember cli, how to initialize a service with a controller?

By : Dano
Date : March 29 2020, 07:55 AM
Any of those help I have basically the same thing, though I inject into all controllers.
In the initializer:
code :
//container.typeInjection('component', 'store', 'store:main');
application.register('clock:service', ClockService, { singleton: true });
application.inject('controller', 'clock', 'clock:service');
updateView: {
  // ...
Angular Testing: Spy a function that was executed on the initialize of a controller

Angular Testing: Spy a function that was executed on the initialize of a controller

By : Hisano Kuniyuki
Date : March 29 2020, 07:55 AM
around this issue Your test is failing because spy gets overridden by real function when controller initializes. One way to avoid this is monkey-patching $scope object with custom setter for requestAuthorization property, that could create spy when controller is trying to assign value to this property:
code :
    beforeEach(inject(function($controller, _$rootScope_, _$injector_, _$stateParams_) {
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
        var reqAuthSpy;
        Object.defineProperty($scope, 'requestAuthorization', {
            get: function() {return reqAuthSpy;},
            set: function(fn) {
             reqAuthSpy = jasmine.createSpy('reqAuthSpy');
        $stateParams = _$stateParams_;
        $stateParams.requestAuthorization = true;

        AppCtrl = $controller('AppCtrl',{
            $scope: $scope,
            $stateParams : $stateParams

Initialize service in controller constructor

Initialize service in controller constructor

By : jshack
Date : March 29 2020, 07:55 AM
To fix this issue Autofac provides 3 events.
OnActivating : raised before a component is used OnActivated : raised once a component is fully constructed OnRelease : raised when a component is disposed
code :
       .OnActivating(e => e.Instance.Initialize()); 
Related Posts Related Posts :
  • AngularJS routing not working properly in PhoneGap
  • AngularJS: Create new scope variable name using a variable value
  • Directive scope update does not update controller scope (AngularJs)
  • Unit-test a angular controller function in a directive
  • jQuery Sparkline in a cell in ng-grid using CellTemplate and Directive
  • AngularJS determine filter in controller
  • How to deploy an app using ionic framework to Amazon? (elastic beanstalk prefered)
  • Conditional injection of a service in AngularJS
  • Filtering on object properties not working
  • Open $modal from the service and pass variables to the controller
  • ngClick evaluated against scope instead of isolateScope
  • onEnter and onExit are not called when state is left/activated in angularjs ui router
  • Shared client & server modules with angularjs and pongular
  • Getting the angular app to run when using protractor
  • Angularjs: create a link for download different types of file
  • AngularJS: On Change of Parent DropDown Option Also Changes the Dependant Drop Down Options Associated with Other Parent
  • Angular HTML5 mode, Wordpress, Rewrites, Apache and You
  • Using angular js form validations in <form:input> tag
  • AngularJS call common functions in a directive template
  • AngularJS Promise resolved incorrectly
  • properly clearing whole AngularJS ui-grid chart
  • Can not figure out how to store $rootScope in angular.bootstrap
  • animate.css animation within angularjs 1.3 using ngAnimate gives different animations in Firefox
  • Auth0 IE Issue: Unable to get property 'frames' of undefined or null reference
  • Am I misusing directives with too much logic?
  • Use an Angular JS value as a parameter of TWIG "path" function in Symfony 2
  • multiple function on ng-click angularjs
  • Converting $.ajax call to $http call in angular 404 error
  • Angularjs templateUrl file location not found
  • How to execute the function after change page in angularjs?
  • Pass variable to AngularJS directive without isolated scope
  • OAuth, Custom Login, or Both
  • Angular Directive: Isolate Scope & Transclude is true, but HTML in the directive needs to continue to bind to parent
  • Compare objects in angularjs
  • Combine "Checkbox" & "Avatar" in an Ionic Framework List
  • Issue with the Angular UI router when inheriting from a parent state
  • Why is a parent controller's $scope not accessible from a (child) directive?
  • How to get back button in Ion-tabs page
  • AngularJS bind class attribute to model
  • How can I get AngularJS ui-router to respond correctly to a browser page refresh?
  • Angular validation does not work
  • Breez Create Complex Entities
  • Drag and drop using protractor in dthmlx component
  • In Angular JS, what is the default 'cache' value that is used when sending request with $http service?
  • How to change placeholder for single textbox in angular?
  • $ is not defined - jquery angular mixing
  • CORS issue angularjs and spring security
  • Angularjs cannot select last item in dropdown the first time
  • Angularjs - $watch issue
  • On change radio button to no the span should hide in angular
  • Creating a wordcloud generator in Angularjs
  • Angular UI Bootstrap collapse - whats wrong?
  • AngularJS after redirect from app to app, the default page is not loading
  • Why does my scope update my factory and my factory update my scope when it should not?
  • AngularJS testing $httpBackend.whenGET().respond() not working with number as parameter
  • Setting up Login in Angular App
  • Retrieve value that is stored in db and display in select box using angularjs and laravel
  • Binding nested array in Angular view
  • Using filter with geojson, right direction?
  • Angularjs : mistake with $q
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org