Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS dependency injection of value inside of module.config

Tags:

angularjs

Trying to setup some helpers value to the module. Tried with service and value and it didn't help:

var finance = angular.module('finance', ['finance.services'])     .value("helpers", {         templatePath: function (name) {             return '/areas/scripts/finance/templates/' + name + '/index.html';         }     })     .config(['$routeProvider', 'helpers', function ($routeProvider, helpers) {     $routeProvider.         when('/', {             templateUrl: helpers.getTemplatePath('dashboard'),             controller: DashboardController         })                     .when('/people', {             templateUrl: '/areas/scripts/app/people/index.html',             controller: PeopleController         })         .otherwise({             redirectTo: '/dashboard'         }); }]); 

What I am doing wrong?

like image 537
Andrej Kaurin Avatar asked Oct 15 '12 20:10

Andrej Kaurin


People also ask

How do I inject a module in AngularJS?

Injecting a value into an AngularJS controller function is done simply by adding a parameter with the same name as the value (the first parameter passed to the value() function when the value is defined). Here is an example: var myModule = angular. module("myModule", []); myModule.

Which components Cannot be injected as a dependency in AngularJS?

Note that you cannot inject "providers" into run blocks. The config method accepts a function, which can be injected with "providers" and "constants" as dependencies. Note that you cannot inject "services" or "values" into configuration.

Which components can be injected as dependency in AngularJS?

Which Component can be Injected as a Dependency In AngularJS? In Angular. JS, dependencies are injected by using an “injectable factory method” or “constructor function”. These components can be injected with “service” and “value” components as dependencies.

What is $inject in AngularJS?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.


2 Answers

The problem is that you are trying to inject a value object helpers in the config block of a AngularJS module and this is not allowed. You can only inject constants and providers in the config block.

The AngularJS documentation (section: "Module Loading & Dependencies") gives the insight into this:

A module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. In its simplest form the module consist of collection of two kinds of blocks:

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

like image 153
pkozlowski.opensource Avatar answered Sep 29 '22 11:09

pkozlowski.opensource


Instead of .value you can use .constant. Then you can use your service in .config part.

like image 22
fedor.belov Avatar answered Sep 29 '22 12:09

fedor.belov