Wienczny
Wienczny

Reputation: 4205

How do I get an Angularjs module and invoke .value on it?

I'm trying to use a vertxbus module. It's configuration is set using .value. How do I update these configuration values from my 'appModule'? To my understanding

angular.module('knalli.angular-vertxbus')

should return a reference to the module and .value should change the injected value. I've created a jsFiddle and here is the js used:

'use strict';

var testmodule = angular.module('TestModule', []).value('key', 'module').value('key', 'module2');
    testmodule.factory('MyService', ['key', function (key) {
    return key;
}]);

var module = angular.module('myApp', ['TestModule'])
.run(function () {
    testmodule.value('key', 'run1');
    angular.module('TestModule').value('key', 'run2');
}).controller('MyCtrl', ['$scope', 'MyService', 'key', function ($scope, MyService, key)      {
    $scope.value = MyService;
    $scope.key = key;
}]);

I would expect a result of run2 or at least run1 but module2 is returned. Why?

Upvotes: 1

Views: 616

Answers (2)

Ilan Frumer
Ilan Frumer

Reputation: 32377

$provide shorthands

Under the hood, constant & value & service & factory & provider are all shorthands for the $provide service which is available only during the configuration phase.

From angular.js documentation:

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:

  1. 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.
  2. 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.

your case:

You cannot register providers (in your case value) inside run blocks because they are only available during the configuration phase.

Read more about modules and $provide

Upvotes: 2

knalli
knalli

Reputation: 1993

The actual idea behind .value, .constant or more generally .config is the possibility to change how the modules' components should constructed themselves.

In that case, the angular-translate's vertxBus component will invoke implicitly a SockJS instance which connects implicitly to a server. That means the configuration settings must be known before the application will be running. Because of this, there are two phases (see answer by Ilan Frumer): config and run.

However, because .value and .config are too static and could collide with other components, the angular module angular-vertxbus has an own provider for all the settings (since version 0.5: https://github.com/knalli/angular-vertxbus/releases/tag/v0.5.0).

Starting with 0.5, this could be look like this:

var module = angular.module('app', ['knalli.vertx-eventbus']);
module.config(function(vertxEventBus) {
  vertxEventBus.useDebug(true).useUrlPath('/eventbus');
});

Just like the others, the provider is only available in the config-phase.

Disclaimer: I'm the author of angular-vertxbus

Upvotes: 3

Related Questions