代码很简单:
<!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
函数,我们只在主体中调用它一次。但在控制台中,它打印两次---name---:
:
---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
为什么函数name()
被调用了两次?