I have an angular module which I want to have a dependency injected into it conditionally. i.e.
var myapp = angular.module('myapp', [
'ngRoute',
'myappcontroller',
'ngGrid' // I want to include ngGrid only if I am running a debug version of myapp
]);
Is there any way to do that?
You can, but with a bit of extra work.
The second parameter is an array so nothing prevents you from doing this:
var dev = ['foo', 'bar'];
var prod = ['foo'];
var deps = dev; //or prod
angular.module('foo', []);
angular.module('bar', []);
angular.module('myApp', deps);
Related
I am beginning to work on an AngularJS app, with a single route, /login. Here is the code for the Controller that handles that route.
angular = require('angular');
// require('../resources/loginResource');
var di = ['$scope'];
var controller = function LoginCtrl($scope) {
$scope.myArray = [
"foo",
"bar",
"baz"
];
};
angular.module('myApp', [])
.controller('LoginCtrl', di.concat(controller) );
When the second line in the file, the reference to loginResource, is commented, the page /login appears just fine. But when I uncomment it, the page renders blank. No console errors, nothing in my terminal output.
Here is the loginResource file:
angular = require('angular');
require('angular-resource');
var app = angular.module('myApp', ['ngResource']);
var di = ['$resource'];
var myResource = function Foobar($resource) {
return $resource('http://mockbin.org/bin/c7bb5923-18ec-4e2f-9189-df8fb80b606b');
};
app.factory('Foobar', di.concat(myResource));
I am using gulp and Browserify.
This seems to be the proper way to inject a Service that uses $service. If so, why is the page rendering blank?
The problem will be the repeated definition of the myApp module. In your first snippet, you have:
angular.module('myApp', [])
and in your second, you have:
var app = angular.module('myApp', ['ngResource']);
You need to use a different name for the module in the second snippet and you need to include that name in the configuration of the app module.
Change the first snippet to something like this:
angular = require('angular');
require('../resources/loginResource');
...
angular.module('myApp', ['loginResource']) ...
and change the second to something like this (it's probably a good idea to rename the app variable, too - as it's not the app module):
angular = require('angular');
require('angular-resource');
var mod = angular.module('loginResource', ['ngResource']);
...
mod.factory('Foobar', di.concat(myResource));
I'm new to Angular and am working through various tutorials (Codecademy, thinkster.io and so on) and have seen two ways of declaring the app container. Firstly:
var app = angular.module('myApp', [])
Or simply like this:
angular.module('myApp', [])
Is one better practice over the other or is it simply a different style without any major effect?
There'n no difference in how both the approach works except following
var app = angular.module('myApp', []) will add an extra global variable(If you're hyper conscious about global variables), however this will shorten your code, don't have to repeat angular.module('myApp') multiple times. You can use app instead of angular.module('myApp').xxx at many times.
You can chain the methods as follow, instead of adding a variable
angular.module('myApp', [])
.controller(.....)
.directive(.....)
.factory(.....);
angular.module supports a third parameter, config function. If you chain you can do more than one config, which may be useful if you have a lot of configurations.
angular.module('myModule', [])
.config(function(injectables) {
//config block 1
})
.config(function(injectables) {
//config block 2
});
Instead of:
angular.module('myModule', [], function(){
//only config
});
Also it eliminates risk of creating global variables when creating providers.
There is no difference, only the style and whatever you prefer, see this
angular.module('myApp', [])
.controller("xCtrl", ['$scope', function($scope){
}])
.controller("yCtrl", ['$scope', function($scope){
}])
.controller("zCtrl", ['$scope', function($scope){
}]);
In this case if you put the ; after y controll, z controller will not work. Means all will be treated as a single function. On the other way:
var app = angular.module('myApp', []);
app.controller("xCtrl", ['$scope', function($scope){
}]);
app.controller("yCtrl", ['$scope', function($scope){
}]);
app.controller("zCtrl", ['$scope', function($scope){
}]);
In it every function is independent.
The only reason for setting a global variable referring to your module, is if you want to separate it in to multiple files.
for example:
main.js:
var app = angular.module('myApp', [])
.controller('controller1',.....)
.controller('controller2',.....);
directives.js
app
.directive('directive1',.....)
.directive('directive2',.....);
although,you should know it is not so good to relay on global variables (app is global in this example), because they could get overridden by other librarys, and there are other ways to refer to your module in other files, like in this example:
main.js:
angular.module('myApp', [])
.controller('controller1',.....)
.controller('controller2',.....);
directives.js
angular.module('myApp')
.directive('directive1',.....)
.directive('directive2',.....);
and if it is a big and complex angular application, you might even want to separate it to multiple modules:
main.js:
angular.module('myApp', ['myApp.directives'])
.controller('controller1',.....)
.controller('controller2',.....);
directives.js
angular.module('myApp.directives', [])
.directive('directive1',.....)
.directive('directive2',.....);
https://code.angularjs.org/1.0.0rc9/angular-1.0.0rc9.js
The above link defined external js file i don't know the injector to angular-1.0.0rc9.js so the my app is not run in browser
var myApp = angular.module('myApp', ['what injector:module is define here']);
please help me
You don't need to inject any dependencies to start your app. However you should add dependencies when you need them.
(function () {
'use strict';
var myApp = angular.module('myApp');
})();
Let's say you need the ui.bootstrap dependency, you can modify your code to look like this.
(function () {
'use strict';
var myApp = angular.module('myApp', ['ui.bootstrap']);
})();
Im trying to separate my Angular controllers into different files. Right now each one appears like this.
AddPropertyController.js
angular.module('myApp.controllers.addproperty', [])
.controller(...);
SearchController
angular.module('myApp.controllers.search', [])
.controller(...);
Now for each one of these I had to include them in the Angular myApp. via:
angular.module('myApp', [
'myApp.controllers.search',
'myApp.controllers.addproperty'
])
my question is, is there a way to just include them all at once instead of adding them individually? My first attempt was to name each module the same, angular.module('myApp.controllers',...) then just include myApp.controllers in the App. But that didn't seem to work... I guess Im asking is what is the best way to have separated controller files but in all in one module. Thanks for the help!
Check out the angularjs docs for info on controllers and adding them to app level modules...
For example for your code:
var myApp = angular.module('myApp',[]);
myApp.controller('SearchController', ['$scope', function($scope) {
$scope.search = 'Hola!';
}]);
myApp.controller('AddPropertiesController', ['$scope', function($scope) {
$scope.props = [];
}]);
You're basically attaching each controller to the myApp instance, so the app is aware of the controllers.
I would organize them like this:
MyController1.js
var MyController1 = ['$scope', function($scope) {
}];
MyController2.js
var MyController2 = ['$scope', function($scope) {
}];
MyModule.js
var app = angular.module('MyModule',[]);
app.controller({
MyController1: MyController1,
MyController2: MyController2,
etc
});
This is the similar to how the Angular source code is itself organized:
AngularPublic.js
ng Directives
var myapp = angular.module('myapp',['ngRoute','ui.bootstrap']);
var myapp = angular.module('myapp', ['infinite-scroll']);
I want to use the infinite-scroll http://binarymuse.github.io/ngInfiniteScroll/documentation.html but it seems like I'm doing it the wrong way. How to include the module?
Add it to the original list instead of trying to declare a new module
var myapp = angular.module('myapp',['ngRoute','ui.bootstrap', 'infinite-scroll']);