0
<html ng-app>
<body>
    <form ng-controller="StartUpController">
        Starting:
        <input ng-change="computeNeeded()"
            ng-model="funding.startingEstimate">
        Recommendation: {{funding.needed}}
    </form>
    <script src="angular.min.js"></script>
    <script>
        function StartUpController($scope) {
            $scope.funding = { startingEstimate: 0 };
            $scope.computeNeeded = function () {
                $scope.needed = $scope.startingEstimate * 10;
            };
        }
    </script>
</body>
</html>

在此处输入图像描述

输出是在文本框中输入的值的十倍......但没有收到任何输出。

4

3 回答 3

2

只需更改表达式:

<input ng-model="funding.startingEstimate" />
Recommendation : {{ funding.startingEstimate * 10 }}

无需添加控制器代码

于 2013-07-19T10:14:38.520 回答
2

startingEstimate不在范围内。它在 中funding,它本身就在范围内。

并且该模板还查找neededin funding,而不是在范围内。

所以代码应该是:

$scope.funding.needed = $scope.funding.startingEstimate * 10;
于 2013-07-19T10:13:51.303 回答
1

你的代码应该看起来像这样。

http://jsfiddle.net/nisar_sql/sYWtS/

<body ng-app>
    <form ng-controller="StartUpController">
        Starting:
        <input ng-change="computeNeeded()"
            ng-model="funding.startingEstimate">
        Recommendation: {{funding.needed}}
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
    <script>
        function StartUpController($scope) {
            $scope.funding = { startingEstimate: 0 };
            $scope.computeNeeded = function () {
                //$scope.needed = $scope.startingEstimate * 10;
                $scope.funding.needed = $scope.funding.startingEstimate * 10;
            };
        }
    </script>
</body>
于 2013-07-20T09:59:45.463 回答