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版本之际,我们认为应该着手写一些东西,告诉大家我们在设计上的考虑,以及为什么做这样的改变.现在把这些和大家一起分享,从 ...
随机推荐
- TFS二次开发系列:六、TFS的版本控制
在TFS中对于版本控制是在WorkSpace工作区来控制的. 首先我们先整理WorkSpace的一些基本使用方法. CheckIn:迁入挂起的操作 CreateMapping:创建一个本地映射地址 D ...
- python走起之第十二话
1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...
- TCP/IP协议学习(四) 基于C# Socket的Web服务器---静态资源处理
目录 1. C# Socket通讯 2. HTTP 解析引擎 3. 资源读取和返回 4. 服务器测试和代码下载 Web服务器是Web资源的宿主,它需要处理用户端浏览器的请求,并指定对应的Web资源返回 ...
- 转:详解Eclipse断点
详解Eclipse断点(原) 详解Eclipse断点 大家肯定都用过Eclipse的调试的功能,在调试的过程中自然也无法避免要使用断点(breakpoint),但不知是否对Eclipse中各类断点都有 ...
- 可靠UDP
tcp为我们做了什么事情? 总得来说,tcp做了这几件事: 通过序列号和基于确认的超时重传机制,为上层提供了可靠的字节流服务: 通过滑动窗口.拥塞窗口提供了流量控制: 默认情况下,为了有效利用带宽,t ...
- 数据挖掘算法(四)Apriori算法
参考文献: 关联分析之Apriori算法
- express+gulp构建项目(一)项目目录结构
express是基于nodejs平台的web框架,它可以让我们快速开发出web引用.而gulp是一种自动构建工具,非常强大,有了它,能帮我们完成很多繁琐的工作,例如,静态文件的压缩,为静态文件加上哈希 ...
- python——赋值与深浅拷贝
初学编程的小伙伴都会对于深浅拷贝的用法有些疑问,今天我们就结合python变量存储的特性从内存的角度来谈一谈赋值和深浅拷贝~~~ 预备知识一——python的变量及其存储 在详细的了解python中赋 ...
- java实现的类和表持久化
//映射的过程: package com.ly.orm; import java.lang.reflect.Field; import java.util.ArrayList; import java ...
- 面试中常用的__proto__,prototype和原型链,你都了解了吗?
上一篇随笔主要讲了变量提升的问题,今天我们来讲讲很多前端er在初期很长一段时间内都没有完全搞明白的原型链和构造函数. 1,什么是构造函数 那么要讲到构造函数,必须要有一个函数,所以我们建立一个函数 f ...