Tuesday, 23 June 2015

angular controller basic



angular.module('app', [])
  .controller('xxController', function($scope) {

Above is a very common controller. I am going to explain a little bit more about how angular deal with "function ($scope){ ...." .

Angular will get the function as a string and replace $xxx by the result of calling $xxxProvider.

So if you use any html/javascript minify libraries to minify your javascript and html, your $scope will be changed to something as "a" or "a1" ... When it running it will fail.

So below is better:

var myApp = angular.module('myApp',[]);

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);


in this case you "function ($scope" can be "function (xxxx" anything. because angular replace it according to their sequences.

However, some build plugin or libraries already support that, we don't have to do that, and the plugin will save your code, You still can do as the code on the top of the page.




No comments:

Post a comment