Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS controller syntax - difference between array and function version

I am new to AngularJS. What is difference between a controller declared with an array parameter, listing the dependencies both as strings and as JavaScript names,

app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {

}]);

...and this form, listing just the JavaScript names?

app.controller("firstController", function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService){

});

Why the weird syntax in the first version?

like image 954
Rohit13 Avatar asked Nov 06 '13 04:11

Rohit13


People also ask

What are the basic directives and controllers in AngularJS explain?

In AngularJS, $scope is the application object (the owner of application variables and functions). The controller creates two properties (variables) in the scope (firstName and lastName). The ng-model directives bind the input fields to the controller properties (firstName and lastName).

What is Controlleras in AngularJS?

In AngularJS, a Controller is defined by a JavaScript constructor function that is used to augment the AngularJS Scope. Controllers can be attached to the DOM in different ways.

Who controls the flow of data in AngularJS application?

All the AngularJS application mainly relies on the controllers to control the flow of data in that application. Basically, it controls the data of AngularJS applications and the controller is a Javascript object, created by a standard JavaScript object constructor.

What is controller in JavaScript?

A controller is a function you write to control your data. With a self-written controller, you can modify data anyway you want: Convert to upper case. Convert currencies. Calculate and Summarize.


1 Answers

It's used when you minified JS files. '$scope', '$modal', '$log', 'HttpService', 'FisrtSharedService', 'SecondSharedService' just declares injectors.

app.controller("firstController", ['$scope', '$modal', '$log', 'HttpService',    'FisrtSharedService', 'SecondSharedService', function($scope, $modal, $log, HttpService, FisrtSharedService, SecondSharedService) {

}]);

You also declare injectors like this:

firstController.$inject = ['$scope', '$modal', '$log', 'HttpService',    'FisrtSharedService', 'SecondSharedService'];
app.controller("firstController", function($scope, $modal, $log, HttpService,  FisrtSharedService, SecondSharedService){

});
like image 86
ducdhm Avatar answered Nov 12 '22 05:11

ducdhm