代码很简单:
@H_502_14@
在AngularJS中,包裹在双花括号中的任何东西都是
expression,在摘要循环中至少进行一次评估。
<!doctype html> <html ng-app="plunker" > <head> <Meta charset="utf-8"> <title>AngularJS Plunker</title> <script>document.write("<base href=\"" + document.location + "\" />");</script> <link rel="stylesheet" href="style.css"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js"></script> </head> <body ng-controller="MainCtrl"> Hello {{name()}}! </body> </html> <script> var app = angular.module('plunker',[]); app.controller('MainCtrl',function($scope) { $scope.name= function() { console.log("---name---:" + new Date()); return "Freewind"; }; }); </script>
你可以看到有一个名字函数,我们只在一次调用它在正文中。但在控制台中,它打印两次—名称— ::
---name---:Wed Feb 20 2013 14:38:12 GMT+0800 (中国标准时间) ---name---:Wed Feb 20 2013 14:38:12 GMT+0800 (中国标准时间)
你可以在这里看到一个现场演示:http://plnkr.co/edit/tb8RpnBJZaJ73V73QISC?p=preview
AngularJS通过连续运行摘要循环,直到没有变化。这就是它确保视图是最新的。既然你调用了一个函数,它运行一次来获取一个值,然后再次看到没有改变。在下一个摘要循环中,它将至少运行一次。