Als ich Angular 1.5 gelernt habe, war ich wirklich verwirrt über die Verwendung der Tags.JavaScript Angular 1.5.x Controller
Nämlich, ich kann nicht verstehen, warum es ein "globales" Objekt geben muss, um die Variablen in einem Controller zu enthalten.
JS Bin: http://jsbin.com/puyayocomi/11/edit?html,js,output
Die Frage ist: Warum können wir Variablen nicht direkt im Controller statt eine globale Aufgabe (in diesem Fall ‚Namensraum‘ genannt) geschaffen rufen zu halten alle Controller-Variablen?
index.html:
<!DOCTYPE html>
<html ng-app="todoApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body>
<h2> Angular Controllers </h2>
<!-- Why not : ng-controller="todoCtrl" -->
<div ng-controller="todoCtrl as namespace">
<!-- -->
<ul class="unstyled">
<!-- Why not : ng-repeat="todo in todos" -->
<li ng-repeat="todo in namespace.todos">
<!-- -->
<label class="checkbox">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}">{{todo.text}}. </span>
</label>
</li>
</ul>
</div>
</body>
</html>
JavaScript:
console.log('Hello Script!');
angular.module('todoApp',[]).controller('todoCtrl',function()
{
var namespace = this;
namespace.todos =
[
{ text:'one', done:true },
{ text:'two', done:false }
];
/*
<!-- Why not : -->
var todos =
[
{ text:'one', done:true },
{ text:'two', done:false }
];
*/
})
'$ scope.todos' und' this.todos' sind äquivalent - John Papas eckiger Styleguide favorisiert 'this' - siehe https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md# Stil-y031 –