JavaScript Should I use the array notation in Angular?

I learnt to write angular dependencies needed using the array notation, that way:

var app = angular.module('MyApp', []);
app.controller('MyCtrl', ['$scope', function($scope) {
    $scope.stuff = 'stuff';
}]);

The Angular doc follows this notation, but I see more and more tutorials not using the array notation and just directly passing the controller the function($scope).

Is there any differences between the two ways to do? Or maybe one was implemented in the version two?

Answer:1

You should be using the Array notation

say Tomorrow if you wish to minify your data using a uglify say, it minifies your big variable names but doesn't touch your strings so your statement from

Case 1 with array notation

original

app.controller('MyCtrl', ['$scope', function($scope) {

minified

x.controller('MyCtrl', ['$scope', function(a) {

Here controller knows exactly knows that variable a is $scope

Case 2 without array notation (whereas if you choose not to use it)

original

app.controller('MyCtrl', function($scope) {

minified

x.controller('MyCtrl', function(a){

Now your controller doesn't know what to do with a variable its not $scope for sure

Answer:2

The array notation is important if you plan to minify your code, which you should be doing in production anyway. Stick to using it.

Answer:3

If you're planning to mini your app, yes you MUST use the array notation.

This is because the variables are renamed, so the injector no longer knows what dependencies you're intending to inject.

For example if $scope was renamed a on minification it wouldn't work.

Obviously this means you have to write and maintain more code. Luckily, you can automate this in your build process.

On my project I use grunt and angular-templates.

https://www.npmjs.com/package/grunt-angular-templates

Answer:4

Yes there is a difference. I would recommend continuing to use array notation since not using it will break your dependency references if the application is minified. For more details, read https://docs.angularjs.org/tutorial/step_05 .

Answer:5

Basically you can rename the variables without affecting angulars ability to inject. As others have said, specifically for minification.

var app = angular.module('MyApp', []);
app.controller('MyCtrl', ['$scope', function(s) {
    s.stuff = 'stuff';
}]);
Answer:6

Array notation is used for injecting the dependencies. Difference between this two is if u do not include array notation means u do not need any dependencies.

Best practice is to use [] notation so that u can include dependencies any time in you application.

Answer:7

In a nutshell there are two ways how you can use dependency injection in angular: implicit or explicit.

Implicit DI is .controller('MyCtrl', function(scope, dep1, dep2){

It does not give instructions to $injector on what to include, it bvasically doing something like a reflection to figure it out. If you minify your code it will turn into

`.controller('MyCtrl', function(a, b, c){ `

That is why we are using explicit DI. There are several ways to do so:

  • use array : .controller('MyCtrl', ['$scope','dep1','dep2', function($scope, dep1, dep2){

  • use $inject:

    .controller('MyCtrl', myCtrl)
    
    myCtrl.$inject = ['$scope', 'dep1', 'dep2'] 
    
    function myCtrl($scope, dep1, dep2) {}  
    

In both ways, even if minification renamed function parameters to something, $injector will know what it is expected to inject, as it has original names in the string literals ( which are not affected by minification)

You can also use comment annotations that will tell compiler/transpiler how to handle angular DI , ie it will turn implicit DI into explicit for you, see ng-annotate

I personally prefer second way with .$inject

Answer:8

I have a controller with 2 scopes: app.controller('search', function($scope) { $scope.value1 = ''; $scope.value2 = 'Some text' + $scope.value1; } And an input field: <input type="text" ...

I have a controller with 2 scopes: app.controller('search', function($scope) { $scope.value1 = ''; $scope.value2 = 'Some text' + $scope.value1; } And an input field: <input type="text" ...

  1. angular update scope
  2. angular update scope variable
  3. angular update scope after ajax
  4. angular update scope value
  5. update angular scope from jquery
  6. angular refresh scope
  7. angular change scope variable
  8. angular refresh scope value
  9. angular change scope variable in html
  10. angular refresh scope variable
  11. angular change scope
  12. angular change scope variable in directive
  13. angular change scope outside controller
  14. angular change scope from outside
  15. angular change scope variable value
  16. angular change scope value
  17. angular 1 update scope
  18. angular update parent scope from child
  19. angularjs update scope
  20. angular force update scope variable

I am attempting to create a global array in javascript to use in multiple functions. The array does not display when attempting to callback. var iListCount = 0; var arrListItems = new Array; ...

I am attempting to create a global array in javascript to use in multiple functions. The array does not display when attempting to callback. var iListCount = 0; var arrListItems = new Array; ...

I'm currently working on a page that have one iframe and one script adding a menu bar. The iframe have a script which is long to load, and the other script for the menu have to be add dynamically ...

I'm currently working on a page that have one iframe and one script adding a menu bar. The iframe have a script which is long to load, and the other script for the menu have to be add dynamically ...

  1. jquery append script tag
  2. jquery append script to page
  3. jquery append script to div
  4. jquery append script tag to head
  5. jquery append script and execute
  6. jquery append script
  7. jquery append script not working
  8. jquery append script src
  9. jquery append script async
  10. jquery append script tag to div
  11. jquery append script dynamically
  12. jquery append script to iframe
  13. jquery append script file
  14. jquery append script code
  15. jquery append external script
  16. jquery append run script
  17. jquery append html script
  18. jquery append inline script
  19. jquery append js script
  20. jquery dom append script

I am a first poster here so please excuse my noob-like behavior. I have a button on my website that when pressed should disappear, and a form should be echoed out in its place without a page refresh....

I am a first poster here so please excuse my noob-like behavior. I have a button on my website that when pressed should disappear, and a form should be echoed out in its place without a page refresh....

  1. make button appear on hover
  2. make button appear as link
  3. make button appear javascript
  4. make button appear unity
  5. make button appear at bottom of div
  6. make button appear clickable
  7. make button appear vba