看了一个关于Angularjs的视频,视频内容讲解的是如何制作一款TODO list形式的SPA(Simple Page Application,单页面应用)。为了增强理解,下面写了一篇文章,用以复习巩固。


准备

Angularjs下载

说是下载,其实只要能在我们的页面中引用到Angularjs即可。可以有如下方式。

CDN加速

使用国内的CDN加速服务也是可以的。

<script src="http://code.angularjs.org/angular-1.0.1.min.js"></script>

npm 方式

使用Nodejs的包管理工具也挺方便的,基本上来说两步就搞定了。

首先进入到我们将要写代码的文件夹。

  • 安装Angularjs:

    npm install angular

  • 页面上引入使用:

    <!-- 这个src地址视自己的情况而定-->
    <script src="node_modules/angular/angular.js"></script>

常规方式

常规方式就是我们手动的下载相关的文件,然后手动的引入,由于比较繁琐。这里不再过多的叙述。

Bootstrap下载

作为一款很流行的现代化的前端框架,Bootstrap可谓是风头尽出了。下载地址

知识储备

MVC 架构

Angularjs 核心采用MVC架构,事件驱动应用。我也是刚接触,所以理解的也不是很到位。有错误的话,还望博友指出来。

ng-app

其作为整个单页面的大管家,app 即application,应用的意思。我们的单页面的服务就充当了这么一个app的作用。

一般来说,ng-app 要作为ng-controller的父容器来嵌套。否则可能不会出现预期的结果

ng-controller

控制器,页面上应用的左膀右臂,控制器的存在简化了模块之间的耦合性,使得代码编写的更加规范和简单。

ng-model

模型处理,一般会和页面元素进行绑定输出,实现无刷新的页面效果。

事件基础

ng-click

在我们的单页面应用中,声明了此属性的元素就具备了点击事件的功能。至于调用的是那一部分的函数,其实是和该元素所在的容器内相关的。

也就是说,点击事件对应的函数是书写在相关控制器里面的用于完成特定的功能的代码。

完整代码

下面 贴出来本例详细的代码

main.js

(function(window){
    // 注册一个应用程序主模块
    var todoapp = window.angular.module('todoapp',[]);
    //  注册控制器
    // window.angular.module('todoapp')
    todoapp.controller('maincontroller'
        ,['$scope',function($scope){
            // $scope 作用就是往视图中添加元素
            // 文本框中的数值
            $scope.text = ''; // 会使用双向绑定的数据类型

            // 为方便页面展示,手动添加一串列表
            $scope.todolist = [{
                text:'Angularjs',
                done:false
            },{
                text:'Bootstrap',
                done:false
            }];

            // 添加函数,响应交互
            $scope.add = function(){
                var text = $scope.text.trim();
                if(text) {
                    $scope.todolist.push({
                        text:text,
                        done:false
                    });
                    $scope.text = '';
                }
            }

            // 点击删除按钮的响应事件
            $scope.delete = function(todo){
                var index = $scope.todolist.indexOf(todo)
                $scope.todolist.splice(index,1);// 起删除的作用
            }

            // 获取已完成的事件的个数,按照checkbox的选择与否实现
            // 由于页面是动态变化的,所以要使用函数,或者干脆使用模型绑定,但是那样的话会稍微麻烦一点
            $scope.doneCount = function(){
                // 使用filter来实现
                var temp = $scope.todolist.filter(function(item){
                    return item.done;// 返回true表示当前的数据满足条件,事件已完成
                });
                return temp.length;
            }
    }]);

})(window)

todolist.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Angularjs 整合Bootstrap实现任务清单</title>
    <!-- 引入 Bootstrap -->
    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container {
            max-width: 720px;
        }
        .done {
            color: #cca;
        }
        .checkbox {
            margin-right: 12px;
            margin-bottom: 0;
        }
        .done > .checkbox > label > span {
            text-decoration: line-through;
        }
    </style>
    <script src="node_modules/angular/angular.js"></script>
    <script src="myjs/app.js"></script>
</head>
<body >
    <div class="container" ng-app="todoapp">
        <header>
            <h1 class="display-3">TODO LIST</h1>
            <hr></header>
        <!-- 内容部分-->
        <section ng-controller="maincontroller">
            <!--为了实现好看的界面,所以用了表单控制-->
            <form class="input-group input-group-lg">
                <input type="text" class="form-control" ng-model="text" name="">
                <span class="input-group-btn">
                    <button class="btn btn-secondary" ng-click="add()">Add</button>
                </span>
            </form>
            <ul class="list-group" style="padding:12px;">
                <li class="list-group-item" ng-class="{'done':item.done}" ng-repeat="item in todolist" >
                <button type="button" class="close" aria-label="close" ng-click="delete(item)">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" ng-model="item.done">
                            <span>{{item.text }}</span>
                        </label>
                    </div>
                </li>
            </ul>
            <p>
                总共 <strong>{{todolist.length }}</strong>
                个任务,已完成 <strong>{{doneCount()}}</strong>
                个
            </p>
        </section>
    </div>

</body>
</html>

页面效果

代码详解

代码中最外边包裹的一层代码可以很好的起到临时空间的效果,防止命名空间的污染。

(function(window){
    // to do something
})(window)

注意最后面的(window)不可缺少。

创建应用

// 注册一个应用程序主模块
var todoapp = window.angular.module('todoapp',[]);

创建控制器

todoapp.controller('maincontroller'
    // 这里的$scope也就起到了容器的作用,声明了变量的可见范围。
        ,['$scope',function($scope){
            // $scope 作用就是往视图中添加元素
            // 文本框中的数值
            $scope.text = ''; // 会使用双向绑定的数据类型

            // 为方便页面展示,手动添加一串列表
            $scope.todolist = [{
                text:'Angularjs',
                done:false
            },{
                text:'Bootstrap',
                done:false
            }];

    }]);

完善功能函数

// 添加函数,响应交互
            $scope.add = function(){
                var text = $scope.text.trim();
                if(text) {
                    $scope.todolist.push({
                        text:text,
                        done:false
                    });
                    $scope.text = '';
                }
            }

            // 点击删除按钮的响应事件
            $scope.delete = function(todo){
                var index = $scope.todolist.indexOf(todo)
                $scope.todolist.splice(index,1);// 起删除的作用
            }

            // 获取已完成的事件的个数,按照checkbox的选择与否实现
            // 由于页面是动态变化的,所以要使用函数,或者干脆使用模型绑定,但是那样的话会稍微麻烦一点
            $scope.doneCount = function(){
                // 使用filter来实现
                var temp = $scope.todolist.filter(function(item){
                    return item.done;// 返回true表示当前的数据满足条件,事件已完成
                });
                return temp.length;
            }

总结

代码不多,思想很深邃。更多内容可以参照

Angularjs 中文学习手册

Angularjs + Bootstrap 制作的一个TODO List的更多相关文章

  1. bootstrap制作搜索框及添加回车搜索事件

    下面是开发中用bootstrap制作的一个搜索框,以及给搜索框添加回车搜索事件的一个小案例. bootstrap制作搜索框及添加回车搜索事件 下面是功能实现的代码: <!DOCTYPE html ...

  2. AngularJS Bootstrap

    AngularJS 的首选样式表是 Bootstrap. 可以在 AngularJS 应用中加入 Twitter Bootstrap,你可以在你的 <head>元素中添加如下代码: < ...

  3. Play1+angularjs+bootstrap ++ (idea + livereload)

    我的web开发最强组合:Play1+angularjs+bootstrap ++ (idea + livereload) 时间 2012-12-26 20:57:26  Freewind.me原文   ...

  4. AngularJS -- Bootstrap(启动器)(转载)

    AngularJS -- Bootstrap(启动器)   点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Bootstrap(初始化) ...

  5. AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框

    AngularJS+BootStrap实现弹出对话框 参考资料: http://angular-ui.github.io/bootstrap/#/modal https://www.zybuluo.c ...

  6. 【17】AngularJS Bootstrap

    AngularJS Bootstrap AngularJS 的首选样式表是 Twitter Bootstrap, Twitter Bootstrap 是目前最受欢迎的前端框架. Bootstrap 你 ...

  7. bootstrap做了一个表格

    花了一下午做了一个表格: 大致是这样: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf ...

  8. 模仿京东顶部搜索条效果制作的一个小demo

    最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...

  9. 使用css3和伪元素制作的一个立体导航条

    使用css3和伪元素制作的一个立体导航条供大家参考,代码如下: <!doctype html> <html lang="en"> <head> ...

随机推荐

  1. javascript/TypeScript 生成GUID

    export const guid = () => { return `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, functi ...

  2. [LeetCode] Longest Harmonious Subsequence 最长和谐子序列

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  3. 跨域访问 - 跨域请求 同源策略概念对跨域请求的影响 及几种解决跨域请求的方法如 jsonp

    为什么会设置同源策略 > 适用于浏览器的一种资源访问策略 > 同源策略(Same origin policy)是一种约定,它是浏览器最核 心也最 基本的安全功能,如果缺少了同源策略,则浏览 ...

  4. 页面中引入mui 地址选择,点击页面中其他input时页面回到顶部

    问题:在页面中引入mui地址选择时,点击页面中的input页面会滚到顶部(谷歌浏览器中出现的bug),在手机上点击input会出现跳动.开始的时候是想修改mui.min.js里的滚动事件,但是后来找到 ...

  5. phpcmsV9.5.8 后台拿shell

    参考url:https://xianzhi.aliyun.com/forum/read/1507.html poc:index.php??m=content&c=content&a=p ...

  6. poj 2653 线段与线段相交

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11884   Accepted: 4499 D ...

  7. hdu 1754 线段树(Max+单点修改)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. Windows、Unix、Linux是什么类型的操作系统?

    Windows:具有图形用户界面的视窗操作系统. Unix:多用户分时操作系统. Linux:类似Unix操作系统,用于个人计算机.

  9. SpringBoot 使用MultipartFile上传文件相关问题解决方案

    1.当上传时未配置上传内容大小,会报错[org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException ...

  10. Python小代码_12_生成前 n 行杨辉三角

    def demo(t): print([1]) print([1, 1]) line = [1, 1] for i in range(2, t): r = [] for j in range(0, l ...