0

I am new to angularjs. I see that people write code like below

angular.module('phonecat', ['dotjem.routing']).
  config(['$stateProvider', function($stateProvider) {
  $stateProvider
      .state('phones', { views: { 'main': { template: 'phones.html' } })
      .state('tablets', { views: { 'main': { template: 'tablets.html' } });
}]);

I understand that [] is for injecting dependencies. I know that we are injecting $stateProvider. But whats the use of writing the second parameter i.e. the function. Is is just syntax and serve any specific purpose?

1 Answer 1

1

The first one is a string literal. The sole purpose of this is to avoid problems with minification and/or obfuscation. The string would survive that.

From there, they just match up with the actual function arguments 1-to-1. You could name the $stateProvider anything you want at this point. As long as the string matches the actual name of the service. The only important thing is that they are in the right order.

This code would work fine:

angular.module('phonecat', ['dotjem.routing']).
  config(['$stateProvider', function(pizza) {
  pizza
      .state('phones', { views: { 'main': { template: 'phones.html' } })
      .state('tablets', { views: { 'main': { template: 'tablets.html' } });
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

@Hacker, always. If you aren't doing it by hand, you should have a build process that takes care of it for you (like ng-annotate, gulp/grunt plugins, etc.). Otherwise you can't safely minify your code (which you should always be doing in production).
So you mean to say writing .controller('myCtrl', function ($scope, $http, $filter, $rootScope,$cookieStore, $sce) is not preferred mode ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.