我尝试了刚刚出版的O'Reily书中的以下示例:
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
$scope.needed = $scope.startingEstimate * 10;
};
}
代码似乎不起作用,我不知道为什么。有人能看出它有什么问题吗?
答案 0 :(得分:0)
是的,你在这里遇到一些问题。
$scope.startingEstimate
不存在
startingEstimate
代替$scope.funding
。要访问它,您需要编写$scope.funding.startingEstimate
。如果您不熟悉,可能需要阅读本文以获取有关对象如何在JavaScript中工作的信息:https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects
funding.needed
不存在
您需要使用{{needed}}
,因为您分配的是$scope.needed
,而不是$scope.funding.needed
。
您的代码可能看起来像这样并起作用:
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()"
ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
function StartUpController($scope) {
$scope.funding = { startingEstimate: 0 };
$scope.computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
}