Monday, 22 June 2015

angular module

angular.module("xxxx",[])  VS  angular.module("xxxx") 

angular.module("xxxx",[]) this line of code will create a module which is a group of code. This is code is the source code for a ng-app. The ng-app can only call functions in this module.

this module object can be change after that, like that:

angular.module("xxxx",[]).controller("cheeseController111", function($scope) {
    $scope.cheese = {}
  });

It is very confuse because if you do this again later:

angular.module("xxxx",[]).controller("cheeseController2222", function($scope) {
    $scope.cheese = {}
  });

That override the previous module, which means you cannot get "cheeseController111".

If you call:

angular.module("xxxx",[]);

You empty the module.

But below code is interesting, because it doesn't override the module, it put more controller into the module (of cause if it use a same controller name, it will override the controller only):

angular.module("xxxx").controller("cheeseController3333", function($scope) {
    $scope.cheese = {}
  });

This is the way we put different controllers into different files.

The only different is "[]"


-----------------------

Below code is interesting:

angular.module('app', ['bpp']);

angular.module('bpp',[]).controller("cheeseController111", function($scope) {
    $scope.cheese = {}
  });  /// this make app has "cheeseController111"

  angular.module('app').controller("cheeseController22", function($scope) {
    $scope.cheese = {}
  });// this makes app has "cheeseController22"

  angular.module('bpp',[]).controller("cheeseController", function($scope) {
    $scope.cheese = {}
  });// this remove all bpp's stuff and just give it a controller--"cheeseController"



Eventually, "app" will only has "cheeseController22" and  "cheeseController".



---------------------------------------------------
---------------------------------------------------

Things in "[]" is a dependency list:

angular.module('app', ['bpp']);

angular.module('bpp',[]).controller("cheeseController111", function($scope) {
    $scope.cheese = {}
  });


Note:  In above code, "app" depends on "bpp". what angular does is angular will copy all bpp's "parameters" (includes controllers, services, filters) into "app".




No comments:

Post a comment