在以下示例中:
的test.html
<!DOCTYPE html>
<html ng-app ng-controller="AppController">
<head>
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="script1.js"></script>
</head>
<body>
<div ng-include="'test1.html'"></div>
{{testFn()}}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
test1.html
<div>{{testFn()}}</div>
Run Code Online (Sandbox Code Playgroud)
script1.js
function AppController($scope) {
$scope.testFn = function() {
console.log("TestFn");
return "test";
}
}
Run Code Online (Sandbox Code Playgroud)
发生fn testFn执行4次.我希望在控制台中只看到2个日志.甚至,如果我删除
<div ng-include="'test1.html'"></div>
Run Code Online (Sandbox Code Playgroud)
有2个日志,而不仅仅是一个.我错了什么?
更新:
angular.js
// added console.log to original code
var ngBindDirective = ngDirective(function(scope, element, attr) {
console.log("ngDirective", attr.ngBind);
element.addClass('ng-binding').data('$binding', attr.ngBind);
scope.$watch(attr.ngBind, function ngBindWatchAction(value) {
console.log("ngDirective - scope.$watch", value);
element.text(value == undefined ? '' : value);
});
});
Run Code Online (Sandbox Code Playgroud)
的test.html
<!DOCTYPE html> …Run Code Online (Sandbox Code Playgroud)