angular初步认识一
最近比较流行MVC前端框架开发,最近研究了一个框架AngularJS框架
不说那么多,先上例子,我是个代码控
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>capter1-angular</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.css">
<script src="../js/angular.min.js"></script>
<script type="text/javascript">
var model = {
user:"Lifeng",
items:[{
action:"Buy Flowers",done:false
},{
action:"Get Shoes",done:false
},{
action:"Collect Tickets",done:true
},{
action:"Call Joe",done:false
}]
} var myApp = angular.module("myApp",[]);
myApp.controller("MyCtrl",function($scope){
$scope.todo = model;
})
</script>
</head>
<body ng-controller="MyCtrl">
<div class="page-header">
<h1>{{todo.user}}'s To Do List
<span class="label label-default">{{todo.items.length}}</span>
</h1> </div>
<div class="panel">
<div class="input-group">
<input type="text" class="form-control"/>
<span class="input-group-btn">
<button class="btn bth-default">Add</button>
</span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Done</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in todo.items">
<td>{{item.action}}</td>
<td><input type="checkbox" ng-model="item.done" /></td>
<td>{{item.done}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
看到几个比较特殊的点
1.html标签中多一个ng-app="myApp"
2.body标签中多一个ng-controller="MyCtrl"
3.tr标签中多了ng-repeat="item in todo.items"
4.{{}}是取值表达式
5.script里面多了一些angular.module 以及myApp.controller等方法
1.根据AngularJS的解释是,一个文档中只有一个ng-app的属性,可以说是文档的唯一标识,当angular发现这个标识的时候,下面的文档树都要经过angular编译
2.ng-controller的指令就是作为前端处理业务的controller层
3.作为一个前端或者后端,看到这个就会想到是一个for遍历集合
4.不用说了,就是取元素的值
5.这个两个方法数据绑定和处理的业务逻辑。
这里还提一点,$scope是一个全局变量,还有就是数据双向绑定,里面用到了一个ng-model指令,这个自己也在学习中,希望以后学习中能够知道他们的原理。
下面可以看到$scope是全局,在对象上增加一个方法,可以在html元素上 直接使用这个方法,看标题栏,还有几个事情没有做的计数
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>angularJS学习</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.css">
<script src="../js/angular.min.js"></script>
<script type="text/javascript">
var model = {
user:"Lifeng",
items:[{
action:"Buy Flowers",done:false
},{
action:"Get Shoes",done:false
},{
action:"Collect Tickets",done:true
},{
action:"Call Joe",done:false
}]
} var myApp = angular.module("myApp",[]);
myApp.controller("MyCtrl",function($scope){
$scope.todo = model;
$scope.incompleteCount = function(){
var _count = 0;
angular.forEach($scope.todo.items,function(item){
if(!item.done){
_count++
}
});
return _count;
}
})
</script>
</head>
<body ng-controller="MyCtrl">
<div class="page-header">
<h1>{{todo.user}}'s To Do List
<span class="label label-default" ng-hide="incompleteCount() == 0">{{incompleteCount()}}</span>
</h1> </div>
<div class="panel">
<div class="input-group">
<input type="text" class="form-control"/>
<span class="input-group-btn">
<button class="btn bth-default">Add</button>
</span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Done</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in todo.items">
<td>{{item.action}}</td>
<td><input type="checkbox" ng-model="item.done" /></td>
<td>{{item.done}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
自定义过滤器和发送ajax请求,写了一下代码,感觉使用挺简单,过滤器接受一个参数,list是angularJS提供的集合数据
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>angularJS自定义过滤器学习</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.css">
<script src="../js/angular.min.js"></script>
<script type="text/javascript">
var model = {
user:"Lifeng" } var myApp = angular.module("myApp",[]); myApp.run(function($http){
$http.get("/angular/todo.json").success(function(data){
model.items = data;
})
}); myApp.filter("checkedItems",function(){
return function(list,showComplete){
var resultArr = [];
angular.forEach(list,function(item){
if (!item.done || showComplete ) {
resultArr.push(item);
};
});
return resultArr;
}
}); myApp.controller("MyCtrl",function($scope){
$scope.todo = model;
$scope.incompleteCount = function(){
var _count = 0;
angular.forEach($scope.todo.items,function(item){
if(!item.done){
_count++
}
});
return _count;
}
})
</script>
</head>
<body ng-controller="MyCtrl">
<div class="page-header">
<h1>{{todo.user}}'s To Do List
<span class="label label-default" ng-hide="incompleteCount() == 0">{{incompleteCount()}}</span>
</h1> </div>
<div class="panel">
<div class="input-group">
<input type="text" class="form-control"/>
<span class="input-group-btn">
<button class="btn bth-default">Add</button>
</span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Done</th>
<th> </th>
</tr>
</thead>
<tbody>
<!--普通的过滤器可以像下面那样 -->
<!--<tr ng-repeat="item in todo.items | filter:{done:false}} | orderBy:'action'"> -->
<!--自定义过滤器可以像下面那样 -->
<tr ng-repeat="item in todo.items | checkedItems:showComplete | orderBy:'action'">
<td>{{item.action}}</td>
<td><input type="checkbox" ng-model="item.done" /></td>
<td>{{item.done}}</td>
</tr>
</tbody>
</table>
<div class="checkbox-inline"><label><input type="checkbox" ng-model="showComplete" />Show Complete</label></div>
</div>
</body>
</html>
再来一个好玩一点的,就是点击按钮可以天添加数据到列表中
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>angularJS数据绑定学习</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.css">
<script src="../js/angular.min.js"></script>
<script type="text/javascript">
var model = {
user:"Lifeng" } var myApp = angular.module("myApp",[]); myApp.run(function($http){
$http.get("/angular/todo.json").success(function(data){
model.items = data;
})
}); myApp.filter("checkedItems",function(){
return function(list,showComplete){
var resultArr = [];
angular.forEach(list,function(item){
if (!item.done || showComplete ) {
resultArr.push(item);
};
});
return resultArr;
}
}); myApp.controller("MyCtrl",function($scope){
$scope.todo = model;
$scope.incompleteCount = function(){
var _count = 0;
angular.forEach($scope.todo.items,function(item){
if(!item.done){
_count++
}
});
return _count;
}
$scope.addNewItem = function(actionText){
$scope.todo.items.push({"action":actionText,"done":false});
}
})
</script>
</head>
<body ng-controller="MyCtrl">
<div class="page-header">
<h1>{{todo.user}}'s To Do List
<span class="label label-default" ng-hide="incompleteCount() == 0">{{incompleteCount()}}</span>
</h1> </div>
<div class="panel">
<div class="input-group">
<input type="text" class="form-control" ng-model="actionText"/>
<span class="input-group-btn">
<button class="btn bth-default" ng-click="addNewItem(actionText)">Add</button>
</span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Done</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in todo.items | checkedItems:showComplete | orderBy:'action'">
<td>{{item.action}}</td>
<td><input type="checkbox" ng-model="item.done" /></td>
<td>{{item.done}}</td>
</tr>
</tbody>
</table>
<div class="checkbox-inline"><label><input type="checkbox" ng-model="showComplete" />Show Complete</label></div>
</div>
</body>
</html>
angular初步认识一的更多相关文章
- Angular初步
一.angular.js是什么? angular.js是一个javascript的框架,与jquery是一个级别的,区别是jquery主要是擅长dom操作,而angular主要是擅长绑定数据显示数据. ...
- Angular.js 的初步认识
MVC模式 模型(model)-视图(view)-控制器(controller) Angular.js采用了MVC设计模式的开源js框架 1.如何在angular.js建立自己的模块(model),控 ...
- angular 实现modal windows效果(即模态窗口,半透明的遮罩层),以及bootstrap(css,components,js)的初步学习
废话不说,直接上代码.可直接看效果,对着分析..今天算是bootstrap 入门了,开心.. 突然居然很多事情就是那样,不要太多的畏惧,迈出第一步其实就成功了一半了. <html ng-app= ...
- angular smart-table组件如何定制化之初步研究
table表运用在后台管理用得频繁,一般bootstrap的class="table"就能满足样式需求以及分页需求,但是每个产品经理需求不一样,比如说分页:bootstrap分页实 ...
- Angular 2 + 折腾记 :(7) 初步了解表单:模板驱动及数据驱动及脱坑要点
前言 表单在整个系统中的作用相当重要,这里主要扯下响应表单的实现方式. 首先须要操作表单的模块引入这两个模块. import { FormsModule, ReactiveFormsModule } ...
- 数据的双向绑定 Angular JS
接触AngularJS许了,时常问自己一些问题,如果是我实现它,会在哪些方面选择跟它相同的道路,哪些方面不同.为此,记录了一些思考,给自己回顾,也供他人参考. 初步大致有以下几个方面: 数据双向绑定 ...
- 利用angular结合translate为项目实现国际化
前言 利用H5项目第一版本已经上线,话说有了第一期就有了第二期,这不要为第二期做准备了,老大发话第一件事就要利用Angular JS实现项目的国际化以及后续要借助这个框架来实现其他功能,好吧我表示没怎 ...
- Angular快速入门篇
简介 AngularJS 是一个为动态WEB应用设计的结构框架,提供给大家一种新的开发应用方式,这种方式可以让你扩展HTML的语法,以弥补在构建动态WEB应用时静态文本的不足,从而在web应用程序中使 ...
- Angular 2.0 的设计方法和原则
转载自:Angular 2.0 的设计方法和原则 在开始实现Angular 2.0版本之际,我们认为应该着手写一些东西,告诉大家我们在设计上的考虑,以及为什么做这样的改变.现在把这些和大家一起分享,从 ...
随机推荐
- 剑指offer五:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. /* public class ListNode { int val; ListNode next = n ...
- [转]SpringMVC拦截器简单教程
亲测有用,地址: http://blog.csdn.net/tjcyjd/article/details/7498236
- CentOS6.5 简单配置Nginx + tomcat
1.配置nginx.conf vi /usr/local/nginx/conf/nginx.conf --这是你的安装目录 注:红框地方为任意位置,server在配置文件中已存在 我使用的是两个tom ...
- 有关uploadifive的使用经验
这段时间做了一个项目用到uploadifive上传控件,和uploadify不同,这个控件是基于HTML5的版本而不用支持falsh,因而移动端也可以使用. 整理用过的相关属性与方法: 属性 作用 a ...
- Java 中如何原样输出转义符号
Java 中的转义字符有好几种,常见的有: 八进制转义字符,格式:\ + 1到3位八进制数字,如\1, \20,范围为 \0 ~ \377,即最大值为255. Unicode转义字符,格式:\u + ...
- 使用SQL语句查询每张表的column name
exec sp_columns tableName 上面这行代码可以查出该表所有的column,改为sp_pkeys,可以查出该表的主键.但是我如果想只查出column name,select COL ...
- Nginx安装与使用
前言 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大 ...
- js控制 input框中输入数字时,累计求和
$('.cc input').bind('input propertychange', function(){ var total = 0; $("input").each(fun ...
- Maplace.js – 小巧实用的 jQuery 谷歌地图插件
Maplace.js是一个小的显示谷歌地图的 jQuery 插件,帮助你把谷歌地图嵌入到你的网站,快速在地图位置上创建标记和控制菜单.它需要 jQuery 和谷歌地图 API v3 支持,所以这两个都 ...
- SPSS数据分析—判别分析
判别分析作为一种多元分析技术应用相当广泛,和其他多元分析技术不同,判别分析并没有将降维作为主要任务,而是通过建立判别函数来概括各维度之间的差异,并且根据这个判别函数,将新加入的未知类别的样本进行归类, ...