Syntax doubt in Angularjs

Asked

Viewed 273 times

1

I’m starting in Angular and in some cases I saw that you pass parameters in the bracket of the "module" and in other cases in the "Function", and in the function I’ve seen 2 forms of declaration.

Example Module:

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ui.utils.masks', 'ngCordova', 'angular-md5', 'ngMessages', 'angularSoap'])

Example Function 1:

.config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider){...}

.controller('LoginCtrl', function ($scope, $rootScope, $pouchDB){...}

Example Function 2:

.controller('GreetingController', ['$scope, $any', function($scope, $any){...}

Could someone explain to me:

  • What is the difference between the declaration of Function 1 and Function 2 (why there are these 2 forms and what would be the practical impact in choosing one or the other)?
  • What’s the difference between passing these parameters in the module and functions?
    • Is there any connection between them?
  • What goes on and what can go on in these parameters

Don’t just stick to my questions. The more information, the better, but at first my doubts are those above.

1 answer

2


  • What is the difference between the declaration of Function 1 and Function 2 (why there are these 2 forms and what would be the practical impact in choosing one or the other)?

When Javascript file minification is used, the parameter names are changed to shorter names $Scope => a. When mangle function is enabled, it should be used ['$scope',function($scope){}] in this format, for Angular to recognize the parameter to be injected into the controller.

  • What is the difference between passing these parameters in the module and functions? Is there any connection between them?

The parameters in the module are exactly what external dependencies the module should load. Ex.: app.module('myApp',['ngResource']).controller('AppCtrl',['$scope','$resource',function($scope, $resource))

In the above example I load the ngResource module to be able to use in my controllers.

Any questions, just talk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.