ÐèÒªµÄ֪ʶ
html¡¢js¡¢³õ²½Á˽âangularjs
ÊìϤnpm¡¢gitµÄ²Ù×÷
Æð²½
ÔÚgithubÉϸøÎÒÃÇÌṩÁËÒ»¸ötodomvcµÄÄ£°å£¬ÎÒÃÇ¿ÉÒÔcloneÏÂÀ´£¬Ê¹ÓÃ
°²×°npm,°²×°git£¨¾ßÌå°²×°²»ÔÚÃèÊö£¬Çë×ÔÐаٶȣ©
-
ÔÚ±¾µØÐ½¨Îļþ¼Ðtodomvc£¬´ò¿ªÖն˽øÈ뵱ǰĿ¼£¬Ö´ÐÐÏÂÃæµÄÃüÁî git clone https://github.com/tastejs/todomvc-app-template.git
cd todomvc-app-template/
npm install
npm uninstall todomvc-common --save
npm install angular --save
µ½´ËÏîĿģ°åÒÔ¹¹½¨Íê³É
ʵÏÖ
ʹÓÿª·¢¹¤¾ß´ò¿ªÄ¿Â¼
-
´ò¿ªindex.html,ÔÚ<body>µÄ×îÏÂÃæÌí¼Óangular.jsµÄÒýÓà <script src="node_modules/angular/angular.js"></script>
-
´ò¿ªapp.js½«window¶ÔÏó»»³Éangular¶ÔÏ󣬴úÂë½á¹¹ÈçÏ£º (function (angular) {
'use strict';
})(angular);
-
´´½¨Ò»¸öÄ£¿é£º var myApp = angular.module('MyTodoMvc',[]);
-
´´½¨Ò»¸öController myApp.controller('MainController',['$scope',function ($scope) {
};
-
±©Â¶textµ½Ò³Ãæ $scope.text = "";
-
±©Â¶todosµ½Ò³Ãæ $scope.todos = [
{
id: 1,text: 'ѧϰ',completed: false
},{
id: 2,text: '˯¾õ',{
id: 3,text: 'ÇôúÂë',completed: true
}
];
-
ÔÚindex.htmlÖÐÕÒµ½ÏÔʾÊý¾ÝµÄul£¬ÁôÏÂÒ»¸öul£¬É¾³ýÆäÓàµÄul£¬½øÐдúÂëµÄ¸ÄÔì <li ng-repeat="todo in todos ">
<div class="view">
<input class="toggle" type="checkbox">
<label>{{todo.text}}</label>
<button class="destroy"></button>
</div>
<form>
<input class="edit" ng-model="todo.text">
</form>
</li>
-
Ìí¼Ó¹¦ÄܵÄʵÏÖ£º
ÅжÏÊäÈëµÄÎı¾ÊÇ·ñΪ¿Õ£¬Îª¿Õ²»×ö´¦Àí
¶Ôtodos×öpush²Ù×÷Ìí¼ÓÔªËØ£¬Ìí¼Óºó½«inputÎı¾¿ò±äΪ¿Õ£¬Ìí¼ÓµÄÔªËØÐèÒªÈý¸öÊôÐÔ£ºid£¬text£¬completed
-
¿¼ÂÇid²»ÄÜÖØ¸´µÄÎÊÌâʹÓÃMath.random()È¡Ëæ»úÊý½øÐÐidµÄÉú³É£¬¾ßÌåʵÏÖ´úÂëÈçÏ£º function getId() {
var id = Math.random();
for (var i = 0; i < $scope.todos.length; i++) {
if ($scope.todos[i].id === id) {
id = getId();
break;
}
}
return id;
}
$scope.add = function () {
if (!$scope.text) {
return;
}
$scope.todos.push({
id: getId(),text: $scope.text,completed: false
});
$scope.text = "";
};
<form ng-submit="add()">
<input class="new-todo" placeholder="What needs to be done?" ng-model="text" autofocus>
</form>
-
ɾ³ý¹¦ÄܵÄʵÏÖ
-
±©Â¶·½·¨ $scope.remove = function (id) {
for (var i = 0; i < $scope.todos.length; i++) {
if ($scope.todos[i].id === id) {
$scope.todos.splice(i,1);
break;
}
}
};
-
Ìí¼ÓʹÓà <button class="destroy" ng-click="remove(todo.id)"></button>
-
Çå¿Õ¹¦ÄܵÄʵÏÖ
-
±©Â¶·½·¨ $scope.clear = function () {
var result = [];
for (var i = 0; i < $scope.todos.length; i++) {
if (!$scope.todos[i].completed) {
result.push($scope.todos[i]);
}
}
$scope.todos = result;
};
-
Ìí¼ÓʹÓà <button class="clear-completed" ng-click="clear()">Clear completed</button>
-
¶ÔÇå¿Õ¹¦ÄÜÌí¼ÓÏÞÖÆ£¬ÔÚÓÐÑ¡ÖеÄʱºòÏÔʾ£¬Ã»ÓеÄʱºòÒþ²Ø
-
±©Â¶·½·¨ $scope.existCompleted = function () {
for (var i = 0; i < $scope.todos.length; i++) {
if ($scope.todos[i].completed) {
return true;
}
}
return false;
};
-
Ìí¼ÓʹÓà <button class="clear-completed" ng-click="clear()" ng-show="existCompleted()">Clear completed</button>
-
Ìí¼Ó±à¼
-
±©Â¶·½·¨ $scope.currentEditingId = -1;
$scope.editing = function (id) {
$scope.currentEditingId = id;
};
$scope.save = function () {
$scope.currentEditingId = -1;
};
-
Ìí¼ÓʹÓà <label ng-click="editing(todo.id)">{{todo.text}}</label>
<form ng-submit="save()">
<input class="edit" ng-model="todo.text">
</form>
-
Ìí¼ÓÈ«²¿Ñ¡Öй¦ÄÜ
-
±©Â¶·½·¨ var now = true;
$scope.toggleAll = function () {
for (var i = 0; i < $scope.todos.length; i++) {
$scope.todos[i].completed = now;
}
now = !now;
};
-
Ìí¼ÓʹÓà <input class="toggle-all" type="checkbox" ng-click="toggleAll()">
-
Ìí¼Ó¹ýÂ˹¦ÄÜ
-
±©Â¶·½·¨ $scope.selector = {};
$scope.$location = $location;
$scope.$watch('$location.path()',function (now,old) {
switch (now) {
case '/active':
$scope.selector = {completed: false};
break;
case '/completed':
$scope.selector = {completed: true};
break;
default:
$scope.selector = {};
}
});
$scope.equalCompare = function (source,target) {
return source == target;
};
-
Ìí¼ÓʹÓà <li ng-repeat="todo in todos |filter:selector:equalCompare" ng-class="{completed:todo.completed,editing:todo.id===currentEditingId}"></li>
<ul class="filters">
<li>
<a ng-class="{selected:selector.completed==undefined}" href="#/">All</a>
</li>
<li>
<a ng-class="{selected:selector.completed==false}" href="#/active">Active</a>
</li>
<li>
<a ng-class="{selected:selector.completed==true}" href="#/completed">Completed</a>
</li>
</ul>
-
×îºóµÄÓÅ»¯ <span class="todo-count"><strong>{{todos.length}}</strong> item left</span>
µÚÒ»´ÎÓÅ»¯-ʹÓ÷ÓÉ
-
ʹÓÃangularµÄ·ÓÉ×é¼þ£¬ÔÚÖն˽øÈëtodomvc-app-templateĿ¼£¬ÊäÈëÒÔÏÂÃüÁ npm install angular-route --save
-
ÔÚindex.html Ìí¼ÓÒýÓÃ <script src="node_modules/angular-route/angular-route.js"></script>
-
ÅäÖ÷ÓÉ var myApp = angular.module('MyTodoMvc',['ngRoute']);
myApp.config(['$routeProvider',function ($routeProvider) {
$routeProvider.when('/:stauts?',{
controller: 'MainController',templateUrl: 'main_tmpl'
}).otherwise({redirectTo: '/'});
}]);
-
ÅäÖÃapp.js $scope.selector = {};
var stauts = $routeParams.stauts;
switch (stauts) {
case 'active':
$scope.selector = {completed: false};
break;
case 'completed':
$scope.selector = {completed: true};
break;
default:
$route.updateParams({stauts:''});
$scope.selector = {};
}
µÚ¶þ´ÎÓÅ»¯-·ÖÄ£¿é
ÔÚÏßչʾ
http://www.lovefoods.top/todomvc/indexV3.html
Ô´ÂëÏÂÔØ
https://github.com/guoshiqiufeng/todomvc-app
Ч¹ûչʾ
£¨±à¼£ºÀî´óͬ£©
¡¾ÉùÃ÷¡¿±¾Õ¾ÄÚÈݾùÀ´×ÔÍøÂ磬ÆäÏà¹ØÑÔÂÛ½ö´ú±í×÷Õ߸öÈ˹۵㣬²»´ú±í±¾Õ¾Á¢³¡¡£ÈôÎÞÒâÇÖ·¸µ½ÄúµÄȨÀû£¬Ç뼰ʱÓëÁªÏµÕ¾³¤É¾³ýÏà¹ØÄÚÈÝ!
|