I have written a module in angularJS that encapsulates all the backend communications. For greater flexibility I have the api prefix as a constant value on the module (could be value since I am not using it in the config phase).
so something like
angular.module('myapp.data').constant('apiPrefix', '/api/data');
Now I want to use this module from two different applications. One uses /api1/data and the other one /api2/data and I would like to change this during the config phase of the application.
I know how to do that with a provider, but having a provider to hold a value seems like an overkill to me. Can I modify used modules constants or values from the application config phase?
something like:
angular.module("data", [])
.value('apiPrefix', '/api/data')
.factory('display', function(apiPrefix){
return {
pref: function(){
console.log(apiPrefix);
return apiPrefix;
}
}
});
angular.module("myApp",['data'])
.config(['apiPrefix', function(prefix){
prefix = 'https:/api/data';
}])
.controller("Example", function($scope, display) {
$scope.prefix = display.pref;
});
to override the module values, you can redefine the angular value in later modules. I believe it should not be done module config time.
angular.module("data", [])
.value('apiPrefix', '/api/data')
.factory('Display', function(apiPrefix){
return {
pref: function(){
return apiPrefix;
}
}
});
angular.module('myapp', ['data'])
.value('apiPrefix', '/api2/data')
.controller('MainCtrl', function($scope, Display) {
$scope.name = Display.pref();
});
see the plunker here:
http://plnkr.co/edit/k806WE
same thing is applicable for angular constants too.
Our module
angular.module("data", [])
.constant('apiPrefix', '/api/data');
We can override constant fully, like value.
angular.module('myapp', ['data'])
.constant('apiPrefix', '/api2/data');
also we can override fully in config
angular.module('myapp', ['data'])
.config(function ($provide) {
$provide.constant('apiPrefix', '/api2/data');
});
also we can override fully or partially (if object) in run
angular.module('myapp', ['data'])
.run(function (apiPrefix) {
apiPrefix = '/api2/data';
});
But if we want to override constant with object partially in config (not in run), we can do something like this
angular.module("module1", [])
.constant('myConfig', {
param1: 'value1' ,
param2: 'value2'
});
angular.module('myapp', ['data'])
.config(function ($provide, myConfig) {
$provide.constant(
'myConfig',
angular.extend(myConfig, {param2: 'value2_1'});
);
});
Angular modules, controllers, etc. can be contained within functions, if-statements, etc. They do not have to be at the top level. So, you could include this in your code:
if (environmentOne()) {
module.value('apiPrefix','api1/data');
} else {
module.value('apiPrefix','api2/data');
}
Hope that helps!
Related
I have a constant which is injected into a controller and I need to write a test which changes this constant and expects different results. I can use $provide to mock the constant but according to articles I've found online, I need to do it in the module declaration, which I believe is like this:
beforeEach(module("someModule"));
beforeEach(function () {
module(function ($provide) {
$provide.constant('someConstant', false);
});
});
I later load the controller like this:
function createController() {
view = $controller(
"someController",
{
$scope: $injector.get("$rootScope").$new()
});
}
Where $controller, $scope and $injector are all injected in my main beforeEach
This does provide the constant and it does change if I change the value in my beforeEach. But only for the entire test suite. I want to change this constant in a describe or an it but I'm not sure how. If I move the $provide down to a describe or it, I get the error:
Error: Injector already created, can not register a module!
I could just create a new file and that is probably what I'm going to do but is there a way I can $provide a dynamic value?
Lets consider such a code
controller
angular.module('someModule', [])
.controller('someController', function($scope, someConstant) {
$scope.someProvidedValue = someConstant;
})
and test for it
controller spec
describe('module', function () {
beforeEach(module("someModule"));
var createController;
beforeEach(inject(function (_$controller_, _$injector_) {
scope = _$injector_.get("$rootScope").$new()
createController = function createController(scope, obj) {
_$controller_("someController", {
$scope: scope,
someConstant: obj.someConstant
});
}
}))
it('someConstant', function () {
expect(scope.someProvidedValue).toBe(undefined)
createController(scope, {
someConstant: false
})
expect(scope.someProvidedValue).toBe(false)
})
it('someConstant', function () {
expect(scope.someProvidedValue).toBe(undefined)
createController(scope, {
someConstant: true
})
expect(scope.someProvidedValue).toBe(true)
})
})
In the mean time I'm looking for looks nicer solution.
I have two AngularJS services:
service1.js
'use strict';
angular.module('myModule', [])
.factory('myService1', function() {
return {
myFunction: function(options) {
if (options) {
// do stuff
}
}
};
});
service2.js
'use strict';
angular.module('myModule', [])
.factory('myService2', function() {
return {
myFunction: function(options) {
if (options) {
// do stuff
}
}
};
});
I learned the hard way from the official docs that this approach will basically remove myService1 because the module gets overwritten. My question is, do I have any options here? I would really like to be able to have my services defined in separate files. Yet, I only want one module.
Thank you for any insights.
You have to define the module just once:
angular.module('myModule', []);
And then use it as many times as you need it without setting the dependencies:
angular.module('myModule');
So either you define the module in a separate file, or if you define it in the first service, the second should look like this:
angular.module('myModule')
.factory('myService2', function() { ... });
This is a follow-up question to How to create this global constant to be shared among controllers in Angularjs?
The answer provided allows a constant $webroot to be shared among controllers.
app = angular.module('myApp', []);
app.constant('$webroot', 'localhost/webroot/app');
app.controller('myController', ['$scope', '$webroot', function($scope, $webroot) {
$scope.webroot = $webroot;
}]);
However, the problem is if I have 10 constants, then all 10 constants have to be injected into the controller. This makes the controller declaration look long and ugly. How can I create an object with properties that are sharable among controllers in AngularJS? In this way, I need only inject a single object instead of many constants. Can this be done in Angularjs? Thanks.
var app = angular.module('app', []);
app.constant('config', {
prop1: 'val1',
prop2: 'val2',
...
});
app.controller('Ctrl', ['config', function(config) {
console.log(config.prop1);
console.log(config.prop2);
...
}]);
You can use a factory for that:
app = angular.module('myApp', []);
app.factory('MyGlobals', function() {
return {
globalOne: 12345,
globalTwo: 'Hey there',
globalThree: {foo: 'bar'},
globalFour: [1, 2, 3, 4],
isFoo: function () {
return (this.globalTwo == 'Foo' ? true : false);
}
}
});
app.controller('myController', ['$scope', 'MyGlobals', function($scope, MyGlobals) {
$scope.globalOne = MyGlobals.globalOne
[...]
if (MyGlobals.isFoo()) {
// Be creative
}
}]);
You can share object or variable among different controllers in multiple ways -
using factories
using services
broadcast or emit
If your requirement is just sharing variables among multiple controllers, you can achieve this using services.
create a service as below - here name is sharedService. you can use your own service name. And then define/declare variables using 'this' keyword (as many you want).
var app = angular.module('app', []);
app.service('sharedService', function ($rootScope) {
this.globalvar1 = "my global variable1";
this.globalvar2="";
});
Inject service in your controller and then access the service variable as below
app.controller('myController', ['$scope', 'sharedService',
function ($scope,sharedService) {
var myGlobalVariable=sharedService.globalvar1;
sharedService.globalvar1="my global variable value changed from controller";
}]);
You can use service variables in all of your controllers but you have to inject service name in controllers
This post was based on this.
My intention is to separate components on a file basis. For example, I want a specific controller to have it's own file (Same goes with services, filters and directives). Of course, files will be group together based on the module they will fall into. Here's an overview of what I currently have:
Directory
User/
User/UserModule.js
User/UserDirective.js
User/UserService.js
User/UserFilter.js
User/UserController.js
UserModules.js
UserModule = angular.module('UserModule', []);
UserModule.controller('userCtrl', ['$scope', 'UserService', UserCtrl])
.factory('userService', function() {
return new UserService();
})
.filter('userFilter', UserFilter)
.directive('userDirective', UserDirective);
UserController.js
UserCtrl = function($scope, UserService) {
// ...
};
UserDirective.js
UserDirective = function() {
return {
// ...
}
};
UserService.js
UserService = function() {
// ...
};
UserFilter.js
UserFilter = function() {
return function() {
// ...
}
};
Then I'll just push the user module to the app module.
app.requires.push('UserModule');
My concern lies on the registration of the concepts (Such as controllers, services...) to the module. I was wondering if this is the best way to go and if it's correct. Also possible issues on the parameters and the external js file.
Consider this part:
.controller('userCtrl', ['$scope', 'UserService', UserCtrl])
The UserCtrl above refers to a function defined in a separate file. Will I be able to pass the $scope and UserService dependency as parameters to the UserCtrl?
UserCtrl = function($scope, UserService) { // Pass parameters (UserController.js)
What's the correct way of doing this in terms of Services, Filters and Directives?
Finally, how can I improve the code?
I'm also using Meteor btw.
You do not need to declare global variables UserModule, UserDirective, UserService. You just need to declare/register them as below. Angular will take care of injecting dependencies.
UserModule.js
angular.module('UserModule', []);
UserDirective.js
angular.module('UserModule').directive('userDirective', function() {
return {
// ...
}
});
UserService.js
angular.module('UserModule').service('UserService', function() {
});
UserController.js
angular.module('UserModule').controller('UserController', ['$scope', 'UserService', function($scope, UserService){
}])
I am using this strategy to lazy-load stuff with RequireJS in my AngularJS app:
define([
'src/services/dependency_resolver', // resolves promise when dependencies are `require`d
'json!modules.json'
], function (dependencyResolver, modules) {
var app = angular.module('myApp', [ 'ngRoute' ]);
app.config(function ($controllerProvider, $routeProvider) {
app.lazy = {
controller: $controllerProvider.register
// <...> other providers
};
angular.forEach(modules, function (moduleConfig) {
angular.forEach(moduleConfig.routes, function (route) {
$routeProvider.when(route.path, {
templateUrl: route.templateUrl,
controller: route.controller,
resolve: dependencyResolver(moduleConfig.dependencies)
});
});
});
});
return app;
});
But I'm not sure what is the correct way test a lazy-loaded controller. It is registered like this:
define(['src/app'], function (app) {
app.lazy.controller('MainCtrl', function () {
//
});
});
And this is my current spec:
describe('`MainCtrl` controller', function () {
var Ctrl,
$scope;
beforeEach(angular.mock.module('myApp'));
beforeEach(function (done) {
require(['module/main'], done);
});
beforeEach(function () {
angular.mock.inject(function ($rootScope, $controller) {
$scope = $rootScope.$new();
Ctrl = $controller('MainCtrl', {
$scope: $scope
});
});
});
it('should ...', function () {
console.log(Ctrl);
});
});
With this spec, an error occurs when controller is being registered, because app.lazy is undefined.
So the question is how to test such controllers?
Cheers!
I was experiencing a similar problem when writing my unit test using the "lazy" property to register my controller. The problem with this approach is that when in the context of a unit test, the module config block will not be executed and as a result, app.lazy will resolve to undefined.
To solve your problem, instead of using provider registration methods to set your properties of app.lazy, the provider registration method should be used to override their counterparts on the module. In other words, your config block should now become:
`app.config(function ($controllerProvider, $routeProvider) {
app.controller = $controllerProvider.register
// <...> other providers
.......
}`
Instead of register your controller using (app.lazy):
`define(['src/app'], function (app) {
app.lazy.controller('MainCtrl', function () {
//
});
});`
you can just define like this:
`define(['src/app'], function (app) {
app.controller('MainCtrl', function () {
//
});
});`
And this should work! Hopefully this can help, and please let me know if this works out or not.
First of all, thank you for the reference you provided - the article is really interesting.
Author of the article is using AngularJs providers to implement his strategy. The thing is, that AngularJs doesn't have providers for 'specs'. So my opinion is that you should omit this strategy in your unit tests.
On this basis, I think, that you should add AMD to your spec file. Define your controller as a dependency in your spec. After this, you may just require all your specs somewhere in main-spec.js and launch your testing framework.