对AngularJs的学习

AngularJS实战
本文通过一个学生信息表的例子,展示了如何使用AngularJS实现数据绑定、筛选和排序等功能,并介绍了AngularJS解决HTML在构建应用上的不足之处。

AngualrJs有什么好处?

AngularJS是为了克服HTML在构建应用上的不足而设计的。

AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷。
AngularJS通过使用我们称为标识符(directives)的结构,让浏览器能够识别新的语法。例如:
  1.使用双大括号{{}}语法进行数据绑定;
  2.使用DOM控制结构来实现迭代或者隐藏DOM片段;
  3.支持表单和表单的验证;
  4.能将逻辑代码关联到相关的DOM元素上;
  5.能将HTML分组成可重用的组件。

构建一个CRUD应用可能用到的全部内容包括:

1.数据绑定、.

2.基本模板标识符、

3.表单验证、

4.路由、

5.深度链接、

6.组件重用、

7.依赖注入。


要用到的东西:

AngularJs下载地址:https://angularjs.org/

Bootstrap下载地址:http://v3.bootcss.com/getting-started/#download

Jquery下载地址:http://www.jq22.com/jquery-info122


接下来,看一下我自己写的一个例子,需要的东西有:

1.bootstrap.css文件

2.angualr.js文件

3.bootstrap.js文件

4.Jquery.js文件

5.studentInfo.js文件(自定义的模拟手机数据的)


我编写html的工具是Hbuilder 写起来比较方便 sublime也是款不错的软件

我们先来看看我的代码:

index.html

<span style="font-size:18px;"><!DOCTYPE html>
<html lang="zh-CN" ng-app>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<title></title>
	
		<link href="css/bootstrap.min.css" />
		<script src="js/jquery.js"></script>
		<script src="js/bootstrap.min.js"></script>
		<script src="js/studentInfo.js"></script>
		<script src="js/angular.js"></script>
	</head>

	<body ng-controller="StudentCtrl">
		<h1>学生信息表   <small>class 1</small></h1>
		Serach:
		<input type="text" ng-model="query" placeholder="Alex" />
		Select
		<select ng-model="orderProp" class="form-control">
			<option value="name">Alex</option>
			<option value="age">21</option>
		</select>
		
		<hr />
		<div class="container-fluid">
			<table class="table table-bordered table-hover table-responsive">
			<tr>
				<th>姓名 </th>
				<th>年龄</th>
				<th>班級</th>
				<th>联系方式</th>
				<th>家庭住址</th>
			</tr>
			<tr ng-repeat="student in students | filter:query | orderBy:orderProp">
				<td>{{student.name}}</td>
				<td>{{student.age}}</td>
				<td>{{student.class}}</td>
				<td>{{student.phone}}</td>
				<td>{{student.adress}}</td>
			</tr>
	</table>
		</div>

	</body>

</html>
</span>


studentInfo.js文件

<span style="font-size:18px;">function StudentCtrl($scope){
$scope.students=[
{
	"name":"Frach",
	"age":23,
	"class":2,
	"phone":"15952855630",
	"adress":"无锡堰桥"
},{
	"name":"Tom",
	"age":21,
	"class":6,
	"phone":"15952855630",
	"adress":"无锡钱桥"
},{
	"name":"Jason",
	"age":22,
	"class":5,
	"phone":"15952855630",
	"adress":"无锡恒隆"
},{
	"name":"Flex",
	"age":19,
	"class":2,
	"phone":"15952855630",
	"adress":"无锡洛社"
},{
	"name":"Alex",
	"age":12,
	"class":12,
	"phone":"15952855630",
	"adress":"无锡钱桥"
}
]
};
</span>

让我们来逐步分析:

1.<html lang="zh-CN" ng-app> 

lang="zh-CN"  这是用于Html5的  

ng-app指令 相当于作用域 就是个范围我能放到<html>标签里,也能放到<div>的标签里,作用域的大小区别。

2.              Serach:
<input type="text" ng-model="query" placeholder="Alex" />
Select
<select ng-model="orderProp" class="form-control">
<option value="name">Alex</option>
<option value="age">21</option>
</select>

<tr ng-repeat="student in students | filter:query | orderBy:orderProp">

1.ng-model 数据绑定  {{ }}双括号绑定的表达式(核心功能 绑定)

2.filter:query   用于筛选 就是过滤数据

3.orderBy:orderProp    排序

以上两个都ng-model了就是数据绑定了  当用户输入什么或者选择了什么  页面都会单页面刷新

4.ng-repeat   这个就像我们的for 或foreach  就是循环遍历集合中每个对象

  <td>{{student.name}}</td>
<td>{{student.age}}</td>
<td>{{student.class}}</td>
<td>{{student.phone}}</td>
<td>{{student.adress}}</td>

上面就是绑定到每个对象的属性在界面上显示出来

那么数据哪里来呢?

<body ng-controller="StudentCtrl"> ng-controller就是把studentInfo.js的模拟手机数据注入进去

从而达到效果

总结

ng-app 作用域

ng-controller  把数据注入进去

ng-repeat   循环

ng-model  绑定数据

{{}}  页面显示绑定数据


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值