Angular的新功能並試圖在書中獲取示例以運行。例子是關於$ watch方法。下面的代碼工作正常:
<html ng-app>
<head>
<title>StartUp Calculator</title>
</head>
<body>
<form ng-controller='StartUpController'>
Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate" />
Recommendation: {{funding.needed}}
</form>
<script src="Scripts/angular.js"></script>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
}
</script>
</body>
</html>
但是,當我添加$表法,簡化模板,整個頁面失敗,出現以下:
沒有startingEstimate顯示在裏面的所有輸入元件
的{{funding.needed}}變量被顯示爲文字串在網頁
失敗的代碼是:
<html ng-app>
<head>
<title>StartUp Calculator</title>
</head>
<body>
<form ng-controller='StartUpController'>
Starting: <input ng-model="funding.startingEstimate" />
Recommendation: {{funding.needed}}
</form>
<script src="Scripts/angular.js"></script>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.$watch('funding.startingEstimate', computeNeeded);
$scope.computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
}
</script>
</body>
</html>
不知道是什麼導致這種情況發生......需要幫助PLZ :-)
JS控制檯錯誤?你能提供一個jsfiddle嗎? btw測試$範圍$ watch($ scope.funding.startingEstimate,computeNeeded);' –