Black
Black

Reputation: 20232

AngularJS - App Controler

On this website i learned to write an app controller for AngularJS like this:

Codecademy Version:

app.controller
(
    'PhotoController', 
    [
        '$scope', 'photos', '$routeParams', 
         function($scope, photos, $routeParams) 
         {   
             photos.success
             (
                 function(data) 
                 {     
                     $scope.detail = data[$routeParams.id];   
                 }
             ); 
         }
    ]
);

But i found that it is also possible to code it like this:

My Version:

app.controller
(
    'PhotoController', 

     function($scope, photos, $routeParams) 
     {   
         photos.success
         (
             function(data) 
             {     
                 $scope.detail = data[$routeParams.id];   
             }
         ); 
     }
);

Both codes work, but what is the difference? Should i use the version from codecademy or mine? And why? My version is shorter and i see no disadvantages.

Upvotes: 1

Views: 50

Answers (2)

Naftali
Naftali

Reputation: 146300

With your second version of your code, it will not work if minified due to the fact that with angular the variables have to be called $scope, photos, $routeParams.

If you do the 1st version it does dependency injection with any variable name.

Upvotes: 1

Vaibhav Pachauri
Vaibhav Pachauri

Reputation: 2671

The problem with your version is that it will not work with minified code. You will have to annotate your controllers and other modules to get it working. Your code will work unless it has not been minified but while minification your service/controllers names will get renamed and break your app.

Please refer to the following link for more information : https://docs.angularjs.org/guide/di

Upvotes: 2

Related Questions