What is the use of square brackets in AngularJs when we are creating components such as controllers, services, etc? I have seen ['ngRoute', 'ngFileUpload'] in examples. What is the use of it?
相关问题
- angularJS: ui-router equivalent to $location.searc
- Separate AngularJS Controllers Into Separate Files
- Angular ngAnimate not working first time on page l
- Ionic Spinner not showing up
- Upload file to Google Cloud Storage using AngularJ
相关文章
- Passing variable through URL with angular js
- Watch entire object (deep watch) with AngularJS
- Angular ng-if change span text
- Can ng-show directive be used with a delay
- AngularJS $routeParams vs $stateParams
- Multiple parameters in AngularJS $resource GET
- How to set class/style of accordion heading in Ang
- PUT to S3 with presigned url gives 403 error
It is used for annotation. If you use any minification or obfuscation software, your injections will be renamed. The annotations in the array will allow angular to map to the correct methods.
There is more info available in the angular doc on
$injector
: https://docs.angularjs.org/api/auto/service/$injectorIt has no special "AngularJS" significance. It represents a Javascript Array and by doing
['ngRoute', 'ngFileUpload']
, you are passing an array of elements (or strings in this case) which are being passed to the function you are calling.Now it is up to the
module
function to decide how it interprets the array you pass. But to answer your question, it is just an Array.Its the way angular dependency injection was defined so you can uglify the source code without breaking it.
For example, a controller may define two dependencies like this:
angular will instantiate the controller with the dependencies as the same order in the array. So it does not the matter the name you give to the arguments. Now imagine that you want to uglify the code to make it like this:
You will still get the dependencies as you are supposed to. However, if you used this notation:
You couldn't uglify the code renaming the function arguments.
The
[]
parameter in the module definition can be used to define dependent modules.example,
The empty array in
angular.module('myApp', [])
. This array is the list of modules myApp depends on.