<!DOCTYPE html>
<html lang="zh_CN">
<head>
<title>字数小例子</title>
</head>
<body ng-app="noCntAPP">
<div ng-controller="noCntCtrl">
<h2>我的笔记</h2>
<textarea cols="30" rows="10" ng-model="txtArea"></textarea> <p><button>读取</button>
<button>提交</button>
<button>撤销</button>
<p>剩余字数: {{getCount()}}</p>
</p>
<div>
<script type="text/javascript" src="./angular.js"></script>
<script type="text/javascript">
angular.module('noCntAPP',[])
.controller('noCntCtrl',['$scope',function($scope){
$scope.txtArea=''; // 初始化文本区域值为空串
$scope.getCount=function(){
return 100 - $scope.txtArea.length;
}
}]);
</script>
</body>
</html>

剩余字数小例子

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<title>字数小例子</title>
</head>
<body ng-app="noCntAPP">
<div ng-controller="noCntCtrl">
<h2>我的笔记</h2>
<textarea cols="30" rows="10" ng-model="txtArea"></textarea> <p><button>读取</button>
<button>提交</button>
<button>撤销</button>
<p>剩余字数: {{getCount()}}</p>
</p>
<div>
<script type="text/javascript" src="./angular.js"></script>
<script type="text/javascript">
angular.module('noCntAPP',[])
.controller('noCntCtrl',['$scope',function($scope){
$scope.txtArea=''; // 初始化文本区域值为空串
$scope.getCount=function(){
if($scope.txtArea.length>100){
$scope.txtArea = $scope.txtArea.slice(0,100); // 超过 100 截取前 0-99 个字符
}
return 100 - $scope.txtArea.length;
}
}]);
</script>
</body>
</html>

改进一下加入字符串长度判断

<!DOCTYPE html>
<html>
<head>
<title>数据存储演示</title>
</head>
<body ng-app="storageApp">
<div ng-controller="storageCtrl">
<h2>我的笔记</h2>
<textarea cols="30" rows="10" ng-model="note"> </textarea>
<p>
<button ng-click="save()">保存</button>
<button ng-click="read()">读取</button>
<button ng-click="truncate()">清空</button>
</p>
<p>{{getRestCount()}}</p>
<script type="text/javascript" src="./angular.js"></script>
<script type="text/javascript">
angular.module('storageApp',[])
.controller('storageCtrl',['$scope',function($scope){
$scope.note=''; //初始化笔记为空串
$scope.getRestCount=function(){
if($scope.note.length>100){
$scope.note=$scope.note.slice(0,100); // 若输入大于100个字符则截取前100个字符
return 100 - $scope.note.length;
}
}
$scope.save=function(){
alert('note is saved!');
localStorage.setItem("note_key",JSON.stringify($scope.note)); //转成json串保存到本地存储
$scope.note='';
}
$scope.read=function(){
$scope.note = JSON.parse(localStorage.getItem('note_key') || '[]'); //处理为 null 的情况
}
$scope.truncate=function(){
localStorage.removeItem('note_key');
$scope.note='';
}
}])
</script>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head><title>备忘示例代码</title></head>
<body ng-app="topAPP"> <div ng-controller="noteCtrl">
<h2>我的备忘</h2>
<p><input type="text" ng-model="newHabit"/><button ng-click="add()">新增</button></p>
<div ng-repeat="habit in habits">
<input type="checkbox" ng-model="habit.isChecked"/>
<span>{{habit.habit}}</span>
</div>
<p><button ng-click="del()">删除选中项</button></p>
<div>
<script src="./angular.js"></script>
<script>
angular.module('topAPP',[])
.controller('noteCtrl',['$scope',function($scope){
$scope.habits=[{habit:'吃饭',isChecked:false},{habit:'睡觉',isChecked:true},{habit:'打豆豆',isChecked:false}];
$scope.add=function(){
console.log($scope.newHabit);
if(!$scope.newHabit){
alert("输入不能为空!");
return;
}
var newObj={
habit:$scope.newHabit,
isChecked:false
};
$scope.habits.unshift(newObj);
$scope.newHabit='';
};
$scope.remove=function(){
$scope.habits.forEach(function(item,index){
if(item.isChecked){
$scope.habits.splice(index,1);
$scope.remove();// 递归防止连续index勾选
}
});
};
$scope.del=function(){
//逆向思维,仅显示未勾选的
var oldHabits=$scope.habits;
$scope.habits=[];// 新建一个空数组
oldHabits.forEach(function(item,index){
if(!item.isChecked){
$scope.habits.push(item);
}
});
}
}]);
</script>
</body>
</html>

angularjs遍历增删

AngularJS 启程三的更多相关文章

  1. AngularJS进阶(三十九)基于项目实战解析ng启动加载过程

    基于项目实战解析ng启动加载过程 前言 在AngularJS项目开发过程中,自己将遇到的问题进行了整理.回过头来总结一下angular的启动过程. 下面以实际项目为例进行简要讲解. 1.载入ng库 2 ...

  2. 现在就开始使用AngularJS的三个重要原因

    现在就开始使用AngularJS的三个重要原因 在线演示1 本地下载 如果你不熟悉什么是Angular.js的话,小编我强烈推荐你阅读 Javascript教程:AngularJS的五个超酷特性.简单 ...

  3. angularJS 服务三

    路由 一 简介 1.路由机制 为了实现无刷新的视图切换,我们通常会用ajax请求从后台取数据,然后套上HTML模板渲染在页面上,然而ajax的一个致命缺点就是导致浏览器后退按钮失效,尽管我们可以在页面 ...

  4. 使用AngularJS的三个重要原因

    入门教程:http://www.ituring.com.cn/minibook/303 : http://angularjs.cn/tag/AngularJS 原因一:Google开发的框架 要知道开 ...

  5. 精通AngularJS(三)深入scope,继承结构,事件系统和生命周期

    深入探讨 Scope 作用域 每一个 $scope 都是类 Scope 的一个实例.类 Scope 拥有可以控制 scope 生命周期的方法,提供事件传播的能力,并支持模板渲染. 作用域的层次结构 让 ...

  6. AngularJS 第三天----作用域

    作用域的概念及其功能 AngularJS使用作用域的概念来充当数据模型的作用,在视图和控制器之间起着桥梁的作用.由于双向绑定的数据特性,视图的修改会更新 $scope,同样对 $scope的修改也会重 ...

  7. angularjs的三目运算

    前言:前几天写代码的时候遇到一个问题,有一个按钮,有"已关注"和"+关注"两种状态,需要对这两种状态的按钮的背景颜色进行区分,单后点击"已关注&quo ...

  8. angularjs笔记(三)

    AngularJS API 7.其他一些常用指令,布尔类型的指令也可以用表达式 (1).数组索引$index <!DOCTYPE html> <html> <head&g ...

  9. AngularJS进阶(三十八)上拉加载问题解决方法

    AngularJS上拉加载问题解决方法 项目中始终存在一个问题:当在搜索栏输入关键词后(见图1),按照既定的业务逻辑应该是服务端接收到请求后,首先返回查询的前7条数据,待客户端出现上拉加载时,继续查找 ...

随机推荐

  1. idea使用actiBPM插件中文乱码

    idea 安转activiti插件后,编辑流程图发现保存后中文乱码,并且idea的字符集(Settings—>Editor—>File Encodings)已经设置为UTF-8,流程图中中 ...

  2. 《杜增强讲Unity之Tanks坦克大战》1-准备工作

    0.案例介绍 0.1开始界面   点击Play Now 进入游戏界面   左边的坦克使用ws控制前后移动,ad键左右旋转,空格键开火   右边的坦克使用方向键上下控制前后移动,方向键左右键实现左右旋转 ...

  3. 阿里云ubuntu16.04安装ruby

    0x0 准备 环境:阿里云轻量服务器ubuntu16.04 目的:安装beef需要的ruby环境 更新软件 sudo apt-get update sudo apt-get upgrade sudo ...

  4. SQL手工注入漏洞测试(Sql Server数据库)

    还是先找到注入点,然后order by找出字段数:4 通过SQL语句中and 1=2 union select 1,2,3……,n联合查询,判断显示的是哪些字段,就是原本显示标题和内容时候的查询字段. ...

  5. git 和 github 链接

    第一步  再电脑上安装git 请自行搜索   到你需要的一个目录下:例如/gittest 首先创建文件,然后  git  add  和 git commit  不然直接查看  git branch - ...

  6. 课堂讨论 alpha版最后总结

    议时间:组队开发最后总结会议   星期一   下午4:30-5:30 会议地点:学院楼自习室 到会人员:唐野野 胡潘华 王永伟 魏孟 会议概要: 1.展示最后开发成果: 2.交流开发过程心得体会: 会 ...

  7. jenkins配置过程中踩过的一些坑

    1,编译通过之后,想要将编译好的war包放到远程服务器上,并解压 unzipBus.sh的脚本如下: #!/bin/bash jar -xvf bus.war 编译后报错:jar:Command no ...

  8. DirectoryEntry_Properties属性的遍历(win2008)

    DirectoryEntry root = new DirectoryEntry(@"IIS://localhost/W3SVC"); string PInfo = "& ...

  9. 手写vue双向绑定数据

    来一张原理图: 实现思路: (1)绑定data 种的数据,为每个数据添加指令.通过Object,defineProperty() 来通知属性是否更改 (2) 找到每个DOM节点的指令.绑定事件.并绑定 ...

  10. 在ubuntu下运行python脚本

    转自http://www.cnblogs.com/hester/p/5575658.html 1. 运行方式一 新建test.py文件: 1 touch test.py 然后vim test.py打开 ...