我正在开发书中的示例应用程序,AngularJS .
在下面的代码中,{{funding.needed}}
没有显示为 10 * startingEstimate
。它按字面意思显示,即未呈现为页面上的 {{funding.needed}}
。
为什么?
<html ng-app>
<body ng-controller="TextController">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">
</script>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0};
$scope.computeNeeded = function() {
$scope.needed = $scope.startingEstimate * 10;
};
}
</script>
</body>
</html>
最佳答案
您需要删除 TextController,因为您尚未定义它,并且它会在出错时阻止加载其他内容。此外,您还需要在某些地方规范您对 $scope.funding 对象的使用,您尝试仅使用没有父引用的成员。以下是一个工作版本(参见它在 http://plnkr.co/edit/Jz95UlOakKLJIqHvkXlp?p=preview 的 plunker 上工作)
<html ng-app>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">
</script>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
<script>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0};
$scope.computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
}
</script>
</body>
</html>
关于javascript - 变量不使用 ng-bind 呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827303/