angualrjs学习总结三(http、select、表格、指令以及事件)
一:http
XMLHttpRequest:$http是angularjs的一个核心服务,用于读取远程服务器的数据。
$http.get(url) 是用于读取服务器数据的函数。
举例:
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost:8080/web/test.jsp")
.success(function(response) {$scope.names = response.records;});
});
</script>
JSON格式字符串:
{
"records":
[
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
}
]
}
二:PHP Ajax 跨域问题最佳解决方案
通过设置Access-Control-Allow-Origin来实现跨域。
例如:客户端的域名是client.runoob.com,而请求的域名是server.runoob.com。
如果直接使用ajax访问,会有以下错误:
XMLHttpRequest cannot load http://server.runoob.com/server.php.
No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.
1、允许单个域名访问
指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:http://client.runoob.com');
2、允许多个域名访问
指定多个域名(http://client1.runoob.com、http://client2.runoob.com等)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
'http://client1.runoob.com',
'http://client2.runoob.com'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}
3、允许所有域名访问
允许所有域名访问则只需在http://server.runoob.com/server.php文件头部添加如下代码:
header('Access-Control-Allow-Origin:*');
三:使用 ng-options 创建选择框
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js">
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<div>
<select ng-model="name" ng-options="x for x in names">{{x}}</select>
</div>
<script>
angular.module('myApp',[]).controller('myCtrl',function($scope){
$scope.name = 'google';
$scope.names = ['google','baidu','360'];
});
</script>
</body>
</html>
四:angularjs在表格中显示数据
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost:8080/web/test.jsp")
.success(function (response) {$scope.names = response.records;});
});
</script>
五:显示序号 ($index) ng-disabled ng-show ng-hide指令
1:添加序号
<table>
<tr ng-repeat="x in names">
<td>{{ $index + 1 }}</td>
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
2:ng-disabled禁用指令
当ng-disabled为true时,则控件将被禁用;
为false时,控件可用。
举例:
<div ng-app="myApp">
<p>
<button ng-disabled="switch">点击</button>
</p>
<p>
<input type="checkbox" ng-model="switch"/>
</p>
<p>
{{switch}}
</p>
</div>
3:ng-show,显示或者隐藏一个html元素
ng-show="true"时,显示元素,为false时,隐藏元素。
<div ng-app="myApp" ng-init="switch=true">
举例:
<p>
<button type="button" ng-show="switch">点击</button>
</p>
<p>
<input type="checkbox" ng-model="switch"/>
</p>
<p>
{{switch}}
</p>
</div>
4:ng-hide,显示或者隐藏一个html元素
ng-hide的值为true时,隐藏元素,ng-hide的值为false时,显示元素
ng-hide的值可以为一个直接量,也可以是一个表达式。
举例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="toggle()">点击</button>
<div ng-hide="myVar">
<p>名:<input type="text" ng-model="firstname"/></p>
<p>姓:<input type="text" ng-model="lastname"/></p>
</div>
<div>{{firstname+" "+lastname}}</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "Tom";
$scope.lastname = "Son";
$scope.myVar = false;
$scope.toggle = function(){
$scope.myVar = !$scope.myVar;
};
});
</script>
</body>
</html>
六:angularjs事件
ng-click:点击事件,点击然后触发事件。
<div ng-app="">
<p>
<button ng-click="count=count+1">点击</button>
</p>
<p>
{{count}}
</p>
</div>
七:人员信息汇总表

css样式部分:
<style>
table{
width: 100%; //实现页面宽度全部展开
height: 100%;
}
table,th,td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
js部分:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
//全部勾选/全部不选
$scope.checkAll = function() {
var el = document.getElementsByTagName('input');
//如果已经是勾选状态
if (el[0].checked == false) {
for (var i = 1; i <= $scope.names.length; i++) {
if ((el[i].type == "checkbox")
&& (el[i].name == "check")) {
el[i].checked = false;
}
}
;
} else {
for (var i = 1; i <= $scope.names.length; i++) {
if ((el[i].type == "checkbox")
&& (el[i].name == "check")) {
el[i].checked = true;
}
}
;
}
};
$http.get('http://localhost:8080/web/test.jsp').success(
function(response) {
$scope.names = response.records;
});
});
</script>
视图html部分:
<body ng-app="myApp" ng-controller="myCtrl">
<div>
<p style="font-family: '微软雅黑'; font-size: '20px'">angularjs表格</p>
<table>
<tr>
<th><input type="checkbox" ng-click="checkAll();" /></th>
<th>序号</th>
<th>姓名</th>
<th>居住地</th>
<th>国籍</th>
<th>操作</th>
</tr>
<tr ng-repeat="x in names | orderBy:'country'">
<td><input id="checkboxes" type="checkbox" name="check" /></td>
<td>{{$index+1}}</td>
<td>{{x.Name}}</td>
<td>{{x.City|lowercase}}</td>
<td>{{x.Country| uppercase }}</td>
<td><a href="#">修改</a> <a href="#">删除</a> <a href="#">添加</a></td>
</tr>
</table>
</div>
</body>
angualrjs学习总结三(http、select、表格、指令以及事件)的更多相关文章
- SQL学习(三)Select语句:返回前多少行数据
在实际工作中,我们可能根据某种排序后,只需要显示前多少条数据,此时就需要根据不同的数据库,使用不同的关键字 一.SQL Server/Access select top 数量/百分比 from tab ...
- CMake学习笔记三:cmake 常用指令
1 基本指令 1,ADD_DEFINITIONS 向 C/C++编译器添加-D 定义,比如: DD_DEFINITIONS(-DENABLE_DEBUG -DABC),参数之间用空格分割. 如果你的代 ...
- C#学习笔记三(委托·lambda表达式和事件,字符串和正则表达式,集合,特殊的集合)
委托和事件的区别 序号 区别 委托 事件 1 是否可以使用=来赋值 是 否 2 是否可以在类外部进行调用 是 否 3 是否是一个类型 是 否,事件修饰的是一个对象 public delegate vo ...
- iView学习笔记(三):表格搜索,过滤及隐藏列操作
iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...
- angular学习笔记(三十)-指令(10)-require和controller
本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...
- angular学习笔记(三十)-指令(7)-compile和link(2)
继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的 ...
- angular学习笔记(三十)-指令(7)-compile和link(1)
这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...
- angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令
在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...
- angular学习笔记(三十)-指令(5)-link
这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...
随机推荐
- winform中设置FormBorderStyle为None后点击任务栏自动最小化实现
在winform编程中,有时候我们可能对窗体样式需要定义,不适用系统自带的样式,这样我们可以设置FormBorderStyle属性为None.但是设置了FormBorderStyle为None后,我们 ...
- SimpleHttpServer的学习(1)
闲来没事,分析一下一个简单的HttpServer github地址: https://github.com/Filirom1/SimpleHTTPServer 实现的功能很简单就是一个FTP服务器 默 ...
- 【暑假】[数学]UVa 10375 Choose and divide
UVa 10375 Choose and divide 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19601 思路 ...
- 动态调用WebService 通用方法Moss 中 传统开发中都可用。
WebService是啥大家都知道了,这里不做过多的解释.通常我们使用WebService的做法基本都是在我们的项目中添加Web引用的方式,首先找到WebService的地址,然后定义命名空间,这样会 ...
- textarea使用注意事项
问题现象: 意外的发现页面中 textarea 标签中的内容缩进了 猜测: CSS影响了? 过程:(辛酸得说说) 查了CSS,并没有发现,CSS是正常的 然后找了一个正常的,跟这个异常的进行了对比,代 ...
- HW6.24
public class Solution { public static void main(String[] args) { int count = 0; int color; int numbe ...
- Django中的Model(操作表)
Model 操作表 一.基本操作 # 增 models.Tb1.objects.create(c1='xx', c2='oo') #增加一条数据,可以接受字典类型数据 **kwargs obj = m ...
- FZU 2176 easy problem (DFS序+树状数组)
对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开 ...
- javascript操作注册表
try{ var shell = new ActiveXObject("WScript.Shell"); //读注册表值var key1 ...
- AVR ATMEGA8 串口USART
avr串口配置很简单,配置就几个寄存器就可以进收发: 但有几点要搞明白的是: 1.串口一但被配置成功IO功能自动被占用,这点与LPC或STM8/32不同(需要寄存配置): 2.没有专门的串口开起或闭关 ...