AngualrJs有什么好处?
AngularJS是为了克服HTML在构建应用上的不足而设计的。
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>
<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 绑定数据
{{}} 页面显示绑定数据