加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular JS和不引人注目的JavaScript

发布时间:2020-12-17 08:13:13 所属栏目:安全 来源:网络整理
导读:我正在学习MVC和Angular,我很好奇的代码如下(从angularjs.org取得) html ng-apphead 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="s
我正在学习MVC和Angular,我很好奇的代码如下(从angularjs.org取得)
<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()这样的代码来实现角度最佳实践?

不引人注目的JavaScript是一个旧的概念,不像以前那样重要。从 Wikipedia,

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网站最好的,

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代码。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读