<html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script> <script src="Scripts/Todo.js" type="text/javascript"></script> <link rel="stylesheet" href="todo.css"> </head> <body> <h2> Todo</h2> <div ng-controller="TodoCtrl"> <span>{{remaining()}} of {{todos.length}} remaining</span> [ <a href="" ng-click="archive()"> archive</a> ] <ul class="unstyled"> <li ng-repeat="todo in todos"> <input type="checkBox" ng-model="todo.done"> <span class="done-{{todo.done}}">{{todo.text}}</span> </li> </ul> <form ng-submit="addTodo()"> <input type="text" ng-model="todoText" size="30" placeholder=""> <input class="btn-primary" type="submit" value="add"> </form> </div> </body>
Todo.js
function TodoCtrl($scope) { $scope.todos = [ { text: 'learn angular',done: true },{ text: 'build an angular app',done: false}]; $scope.addTodo = function () { $scope.todos.push({ text: $scope.todoText,done: false }); $scope.todoText = ''; }; $scope.remaining = function () { var count = 0; angular.forEach($scope.todos,function (todo) { count += todo.done ? 0 : 1; }); return count; }; $scope.archive = function () { var oldTodos = $scope.todos; $scope.todos = []; angular.forEach(oldTodos,function (todo) { if (!todo.done) $scope.todos.push(todo); }); }; }
如果不引人注目的javascript的原则表明我们需要将表现与行为分开,为什么可以通过像ng-click = archive()这样的代码来实现角度最佳实践?
Adherents to “Unobtrusive JavaScript” argue that the purpose of markup
is to describe a document’s structure,not its programmatic behavior
and that combining the two negatively impacts a site’s
maintainability.
可用性不相关,因为没有人看到您的DOM代码,除了您的开发团队。一旦您拥抱角色哲学,内联事件处理程序就更容易维护。虽然,您可以随时使用$watch来保持您的HTML清洁。
正如Angular网站最好的,
@H_301_13@AngularJS lets you extend HTML vocabulary for your application. The
resulting environment is extraordinarily expressive,readable,and
quick to develop.
搜索引擎可能对您的网站进行索引稍微困难一些,但是我们有RSS,Sitemaps和AJAX Crawling来解决这个问题。
除非您为非智能手机市场构建应用程序,否则优雅的降级不再相关。几乎所有用户都在支持现代JavaScript(带垫片)的浏览器上。
辅助功能和角度不冲突。只需确保使用ARIA标签和正确的标记。角度使得编写可测试代码变得更加容易,内置于exception handling。
如果您使用功能污染全球范围,则分离问题是一个问题。角度使得更容易在不影响其他文件或代码的情况下分别维护javascript代码。