javascript - How angular is polluting the global space? -


i new in angularjs, practising on angularjs http://curran.github.io/screencasts/introtoangular/exampleviewer/

in following example polluting global space, didn't understand how polluting global space in example 1 in example 2 not.

example 1

<html ng-app>   <head>     <meta charset="utf-8">     <title>angular.js example</title>     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>     <script>       function namectrl($scope){         $scope.firstname = 'john';         $scope.lastname = 'smith';       }     </script>   </head>   <body ng-controller="namectrl">     first name:<input ng-model="firstname" type="text"/>     <br>     last name:<input ng-model="lastname" type="text"/>     <br>     hello {{firstname}} {{lastname}}   </body> </html> 

example 2

<html ng-app="nameapp">   <head>     <meta charset="utf-8">     <title>angular.js example</title>     <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>     <script>       var nameapp = angular.module('nameapp', []);       nameapp.controller('namectrl', function ($scope){         $scope.firstname = 'john';         $scope.lastname = 'smith';       });     </script>   </head>   <body ng-controller="namectrl">     first name:<input ng-model="firstname" type="text"/>     <br>     last name:<input ng-model="lastname" type="text"/>     <br>     hello {{firstname}} {{lastname}}   </body> </html> 

i want how polluting global space in example 1 example.

both examples add angular global namespace. example 1 adds function namectrl global namespace, qualifies polluting it. however, example 2 adds nameapp global namespace argue both pollute global namespace equally. however, example 2 @ least controller not added global namespace it's considered better form. in other words, if you're going add global namespace, it's better add module , use add controllers. in reality, avoid adding module namespace if wanted to, if want split hairs (and annoy people) argue neither 100% correct if objective avoid polluting global namespace.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -