1

我尝试了刚刚出版的 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;
};
}

笨拙

该代码似乎不起作用,我不知道为什么。谁能看到它有什么问题?

4

1 回答 1

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;
   };
}
于 2013-04-13T06:22:49.110 回答