本篇我们看一下AngularJS中的数据绑定。虽然我们直到这篇才提到数据绑定,但事实上在前面几篇中我们已经非常熟练的运用AngularJS的数据绑定功能了!
ngBind(ng-bind)/ {{ expression }}:
1 <!DOCTYPE >
2 <html>
3 <head>
4 <script src="/Scripts/angular.js"></script>
5 </head>
6 <body ng-app>
7 <input ng-model="yourName" />
8 <p>
9 Hello, {{yourName}}
10 </p>
11 <p>
12 Use ngBind to display: <span ng-bind="yourName"></span>
13 </p>
14 </body>
15 </html>
如果你已经看过前面几篇文章,我相信你已经非常熟悉这样的代码了。AngualrJS中使用ngBind进行数据绑定,但是我们更多的会使用Expression(即{{expression}}这样的写法)替代ngBind,这种写法更简便直观。
AngularJS还提供了其他几种数据绑定方式:
ngBindHtml:
1 <!DOCTYPE >
2 <html>
3 <head>
4 <script src="/Scripts/angular.js"></script>
5 <script src="/Scripts/angular-sanitize.js"></script>
6 <script type="text/javascript">
7 (function () {
8 var app = angular.module('twowayBindingTest', ['ngSanitize']);
9
10 app.controller('myController', ['$scope', function ($scope) {
11 $scope.myHtml = "This is a link: <a href=\"#\">Mylink</a>";
12 }]);
13 })();
14 </script>
15 </head>
16 <body ng-app="twowayBindingTest" ng-controller="myController">
17 <div>
18 <span ng-bind-html="myHtml"></span>
19 </div>
20 </body>
21 </html>
ngBindHtml(ng-bind-html)可以将一个字符串以安全的方式插入到页面中并显示成Html。
ngBindHtml将强制使用angular-santitize服务进行安全检查,由于并非包含在AngualrJS核心库中,因此需要引入angular-santitize.js文件,并在定义ngModule时添加对于ngSantitize的依赖声明。
关于AngularJS的服务我们将在今后再统一讨论,这里就不展开了。
ngBindTemplate:
1 <!DOCTYPE >
2 <html>
3 <head>
4 <script src="/Scripts/angular.js"></script>
5 </head>
6 <body ng-app>
7 Name:<input ng-model="yourName" />
8 <br />
9 Age:<input ng-model="yourAge" />
10 <p>
11 <span ng-bind-template="This is {{yourName}}, I'm {{yourAge}} years old."></span>
12 </p>
13 </body>
14 </html>
ngBindTemplate(ng-bind-template)与ngBind不同之处在于:ngBind只能单个绑定变量,且变量无需使用双括号“{{}}”,而ngBindTemplate则可以绑定一个模板,模板中可以包含多个AngularJS的表达式:“{{expression}}”。
ngNonBindable:
1 <!DOCTYPE >
2 <html>
3 <head>
4 <script src="/Scripts/angular.js"></script>
5 </head>
6 <body ng-app>
7 <div ng-non-bindable>This will not be changed: {{1 + 2}}</div>
8 </body>
9 </html>
当然,如果你页面上正好有"{{ my content }}" 这样的内容,不需要执行AngularJS帮你进行编译和计算,使用ngNonBindable(ng-non-binable),AngularJS会自动忽略该内容。
使用ngModel实现Twoway Binding:
1 <!DOCTYPE >
2 <html>
3 <head>
4 <script src="/Scripts/angular.js"></script>
5 <script type="text/javascript">
6 (function () {
7 var app = angular.module('twowayBindingTest', []);
8
9 app.controller('myController', ['$scope', function ($scope) {
10 $scope.students = [];
11
12 $scope.addStudent = function (stu) {
13 $scope.students.push(stu);
14 $scope.stu = {};
15 };
16
17 $scope.removeStudent = function (index) {
18 $scope.students.splice(index, 1);
19 };
20 }]);
21
22
23 })();
24 </script>
25 </head>
26 <body ng-app="twowayBindingTest" ng-controller="myController">
27 <div>
28 <p>Name:<input ng-model="stu.name"></input></p>
29 <p>Age:<input ng-model="stu.age"></input></p>
30 <input type="button" ng-click="addStudent(stu)" value="Add" />
31 </div>
32 <hr />
33 <div ng-repeat="stu in students">
34 <span ng-hide="editMode">{{stu.name}}</span>
35 <input type="text" ng-show="editMode" ng-model="stu.name" />
36
37 <span ng-hide="editMode">{{stu.age}}</span>
38 <input type="text" ng-show="editMode" ng-model="stu.age" />
39
40 <input type="button" value="Edit" ng-hide="editMode" ng-click="editMode = true" />
41 <input type="button" value="Save" ng-show="editMode" ng-click="editMode = false" />
42 <input type="button" value="Remove" ng-hide="editMode" ng-click="removeStudent($index)" />
43 <hr />
44 </div>
45 </body>
46 </html>
上面的代码就展示了AngularJS中的双向绑定的用法。如果你仔细看完代码并执行一下,就会发现双向绑定的奇妙之处。
<input type="button" value="Edit" ng-hide="editMode" ng-click="editMode = true" /> <input type="button" value="Save" ng-show="editMode" ng-click="editMode = false" />
编辑、保存按钮的代码非常简单,都不需要添加业务逻辑,因为是双向绑定,当改变输入框内的内容并点击Save之后,由于span中的stu.name和stu.age以及$scope.students中相应的记录的name和age指向了相同的ng-model,因此AngularJS会自动完成这三者之间的同步变更。因此你都不需要编写额外的代码去完成编辑、保存这样的行为,这就是双向绑定带来的奇妙体验。
本篇讲述了AngularJS中的数据绑定,是不是很简单但也超级实用呢?
参考资料
AngularJS官方文档:https://docs.angularjs.org/api/
CodeSchool快速入门视频:http://campus.codeschool.com/courses/shaping-up-with-angular-js/intro
CodeProject文章:http://www.codeproject.com/Articles/808257/Part-Data-Binding-In-AngularJS
来源:https://www.cnblogs.com/wushangjue/p/4526439.html