angularJS学习笔记三(菜鸟)
AngularJS 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://www.runoob.com/try/angularjs/data/Customers_JSON.php") .success(function(response) {$scope.names = response.records;}); }); </script>
应用解析:
注意:以上代码的 get 请求是本站的服务器,你不能直接拷贝到你本地运行,会存在跨域问题,解决办法就是将 Customers_JSON.php 的数据拷贝到你自己的服务器上,附:PHP Ajax 跨域问题最佳解决方案。
AngularJS 应用通过 ng-app 定义。应用在 <div> 中执行。
ng-controller 指令设置了 controller 对象 名。
函数 customersController 是一个标准的 JavaScript 对象构造器。
控制器对象有一个属性: $scope.names。
$http.get() 从web服务器上读取静态 JSON 数据。
服务器数据文件为: http://www.runoob.com/try/angularjs/data/Customers_JSON.php。
当从服务端载入 JSON 数据时,$scope.names 变为一个数组。
AngularJS Select(选择框)
AngularJS 可以使用数组或对象创建一个下拉列表选项。
使用 ng-options 创建选择框
在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出
我们也可以使用ng-repeat 指令来创建下拉列表
ng-repeat 指令是通过数组来循环 HTML 代码来创建下拉列表,但 ng-options 指令更适合创建下拉列表,它有以下优势:
使用 ng-options 的选项的一个对象, ng-repeat 是一个字符串。
对比:
<select ng-model="selectedSite">
<option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
</select>
<h1>你选择的是: {{selectedSite}}</h1>
<select ng-model="selectedSite" ng-options="x.site for x in sites">
</select>
<h1>你选择的是: {{selectedSite.site}}</h1>
<p>网址为: {{selectedSite.url}}</p>
当选择值是一个对象时,我们就可以获取更多信息,应用也更灵活。
数据源为对象
前面实例我们使用了数组作为数据源,以下我们将数据对象作为数据源。
AngularJS 表格
ng-repeat 指令可以完美的显示表格。
<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://www.runoob.com/try/angularjs/data/Customers_JSON.php")
.success(function (response) {$scope.names = response.records;});
});
</script>
使用 orderBy 过滤器
排序显示,可以使用 orderBy 过滤器:
使用 uppercase 过滤器
使用 uppercase 过滤器转换为大写:
显示序号 ($index)
表格显示序号可以在 <td> 中添加 $index:
使用 $even 和 $odd
AngularJS SQL
使用 PHP 从 MySQL 中获取数据
<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://www.runoob.com/try/angularjs/data/Customers_MySQL.php")
.success(function (response) {$scope.names = response.records;});
});
</script>
ASP.NET 中执行 SQL 获取数据
<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://www.runoob.com/try/angularjs/data/Customers_SQL.aspx")
.success(function (response) {$scope.names = response.records;});
});
</script>
服务端代码
以下列出了列出了几种服务端代码类型:
- 使用 PHP 和 MySQL。返回 JSON。
- 使用 PHP 和 MS Access。返回 JSON。
- 使用 ASP.NET, VB, 及 MS Access。 返回 JSON。
- 使用 ASP.NET, Razor, 及 SQL Lite。 返回 JSON。
跨域 HTTP 请求
如果你需要从不同的服务器(不同域名)上获取数据就需要使用跨域 HTTP 请求。
跨域请求在网页上非常常见。很多网页从不同服务器上载入 CSS, 图片,Js脚本等。
在现代浏览器中,为了数据的安全,所有请求被严格限制在同一域名下,如果需要调用不同站点的数据,需要通过跨域来解决。
以下的 PHP 代码运行使用的网站进行跨域访问。
更多跨域访问解决方案可参阅:PHP Ajax 跨域问题最佳解决方案。
AngularJS HTML DOM
AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令。
ng-disabled 指令
ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性。
<div ng-app="" ng-init="mySwitch=true"> <p> <button ng-disabled="mySwitch">点我!</button> </p> <p> <input type="checkbox" ng-model="mySwitch">按钮 </p> <p> {{ mySwitch }} </p> </div>
实例讲解:
ng-disabled 指令绑定应用程序数据 "mySwitch" 到 HTML 的 disabled 属性。
ng-model 指令绑定 "mySwitch" 到 HTML input checkbox 元素的内容(value)。
如果 mySwitch 为true, 按钮将不可用:
ng-show 指令
ng-show 指令隐藏或显示一个 HTML 元素。
ng-hide 指令
ng-hide 指令用于隐藏或显示 HTML 元素。
AngularJS 事件
AngularJS 有自己的 HTML 事件指令。
ng-click 指令
ng-click 指令定义了 AngularJS 点击事件。
隐藏 HTML 元素
ng-hide 指令用于设置应用部分是否可见。
ng-hide="true" 设置 HTML 元素不可见。
ng-hide="false" 设置 HTML 元素可见。
<div ng-app="myApp" ng-controller="personCtrl"> <button ng-click="toggle()">>隐藏/显示</button> <p ng-hide="myVar"> 名: <input type="text" ng-model="firstName"><br> 姓名: <input type="text" ng-model="lastName"><br> <br> Full Name: {{firstName + " " + lastName}} </p> </div> <script> var app = angular.module('myApp', []); app.controller('personCtrl', function($scope) { $scope.firstName = "John", $scope.lastName = "Doe" $scope.myVar = false; $scope.toggle = function() { $scope.myVar = !$scope.myVar; }; }); </script>
应用解析:
第一部分 personController与控制器章节类似。
应用有一个默认属性: $scope.myVar = false;
ng-hide 指令设置 <p>元素及两个输入域是否可见, 根据 myVar 的值 (true 或 false) 来设置是否可见。
toggle() 函数用于切换 myVar 变量的值(true 和 false)。
ng-hide="true" 让元素 不可见。
显示 HTML 元素
ng-show 指令可用于设置应用中的一部分是否可见 。
ng-show="false" 可以设置 HTML 元素 不可见。
ng-show="true" 可以以设置 HTML 元素可见。

浙公网安备 33010602011771号