Skip to content

Navigation Menu

Sign up

Commit 451eb2f

Browse files
committed
fix(todoApp): remove global controllers
Angular 1.3.x removes support for global controllers. This brings the examples in line with that best practice.
1 parent 5c34bde commit 451eb2f

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

‎src/index.html‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -486,36 +486,37 @@ <h1>Hello {{yourName}}!</h1>
486486
}
487487
</style>
488488
<script id="todo.js">
489-
function TodoCtrl($scope) {
490-
$scope.todos = [
491-
{text:'learn angular', done:true},
492-
{text:'build an angular app', done:false}];
493-
494-
$scope.addTodo = function() {
495-
$scope.todos.push({text:$scope.todoText, done:false});
496-
$scope.todoText = '';
497-
};
489+
angular.module('todoApp', [])
490+
.controller('TodoController', ['$scope', function($scope) {
491+
$scope.todos = [
492+
{text:'learn angular', done:true},
493+
{text:'build an angular app', done:false}];
494+
495+
$scope.addTodo = function() {
496+
$scope.todos.push({text:$scope.todoText, done:false});
497+
$scope.todoText = '';
498+
};
498499

499-
$scope.remaining = function() {
500-
var count = 0;
501-
angular.forEach($scope.todos, function(todo) {
502-
count += todo.done ? 0 : 1;
503-
});
504-
return count;
505-
};
500+
$scope.remaining = function() {
501+
var count = 0;
502+
angular.forEach($scope.todos, function(todo) {
503+
count += todo.done ? 0 : 1;
504+
});
505+
return count;
506+
};
506507

507-
$scope.archive = function() {
508-
var oldTodos = $scope.todos;
509-
$scope.todos = [];
510-
angular.forEach(oldTodos, function(todo) {
511-
if (!todo.done) $scope.todos.push(todo);
512-
});
513-
};
514-
}
508+
$scope.archive = function() {
509+
var oldTodos = $scope.todos;
510+
$scope.todos = [];
511+
angular.forEach(oldTodos, function(todo) {
512+
if (!todo.done) $scope.todos.push(todo);
513+
});
514+
};
515+
}]);
515516
</script>
516517
<script type="text/ng-template" id="todo.html">
517518
<h2>Todo</h2>
518-
<div ng-controller="TodoCtrl">
519+
<div ng-app="todoApp"ng-controller="TodoController">
519520
<span>{{remaining()}} of {{todos.length}} remaining</span>
520521
[ <a href="" ng-click="archive()">archive</a> ]
521522
<ul class="unstyled">
@@ -536,7 +537,7 @@ <h2>Todo</h2>
536537
{ "ng-app": "Tells AngularJS to be active in this portion of the page. In this case the entire document."
537538
, "angular.min.js": "Load AngularJS."
538539
, "todo.js": "Your behavior lives in JavaScript controllers."
539-
, "ng-controller": "The behavior of content under this element will be managed using the <code>TodoCtrl</code> class defined in <code>todo.js</code>."
540+
, "ng-controller": "The behavior of content under this element will be managed using the <code>TodoController</code> class defined in <code>todo.js</code>."
540541
, "{{remaining()}}": "The <code>{{ }}</code> are a declarative way of specifying data binding locations in the HTML. AngularJS will automatically update this text whenever the <code>remaining()</code> function return value changes."
541542
, "ng-click": "Instead of registering event-handlers, you declare which method on the controller to call. Here clicking the link will call <code>archive()</code> method."
542543
, "ng-repeat": "Use <code>ng-repeat</code> to unroll a collection. Here, for every object in <code>todos</code>, AngularJS will create new copy of the <code>&lt;li&gt;</code> element. When objects are added to the <code>todos</code> collection the <code>ng-repeat</code> automatically adds new <code>&lt;li&gt;</code> elements into the DOM. Similarly when the objects are removed from <code>todos</code> then the corresponding <code>&lt;li&gt;</code> element is removed as well. This is one of the most versatile directives in AngularJS."
@@ -550,7 +551,7 @@ <h2>Todo</h2>
550551
}
551552

552553
, "todo.js":
553-
{ "TodoCtrl": "The controller is the code behind the view. You can clearly see your application behavior because there is no DOM manipulation or framework specific boilerplate. Just simple, readable JavaScript."
554+
{ "TodoController": "The controller is the code behind the view. You can clearly see your application behavior because there is no DOM manipulation or framework specific boilerplate. Just simple, readable JavaScript."
554555
, "$scope": "<code>$scope</code> contains your model data. It is the glue between the controller and the view. The <code>$scope</code> is just one of the services that can be injected into the controller."
555556
, "todos": "We are creating the model with two initial todo items. Notice that you simply assign your model to the <code>$scope</code> and AngularJS reflects the state in the UI automatically. The model data is a Plain-Old-JavaScript-Object no need to wrap it in proxy or accesses the property through special setter methods."
556557
, "addTodo": "We are assigning the behavior into the <code>$scope</code> so that the <code>ng-submit</code> can invoke it."

‎test/angularjs.org.spec.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('Angularjs.org', function () {
117117

118118
describe('Add Some Control', function () {
119119
it('should strike out a todo when clicked', function () {
120-
var el = tractor.findElement(protractor.By.css('[ng-controller="TodoCtrl"] ul >li:nth-child(2) input'));
120+
var el = tractor.findElement(protractor.By.css('[ng-controller="TodoController"] ul >li:nth-child(2) input'));
121121
el.click();
122122
expect(el.getAttribute('value')).toBe('on');
123123
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /