从视图中控制器内部的功能

时间:2015-01-02 09:40:13

标签: angularjs

我是角色js的新手,这是一本书的例子。写作者提到,在chaning资金上,它会显示10个倍数的推荐,你可以在代码中看到。但我们编译这个程序没有在can上显示的推荐。可以帮助我 $ scope.computeNeeded 如何工作..

<html ng-app>
    <head>
        <title>this is title</title>
        <script src='angular.js' type="text/javascript"></script>
    </head>
<body>

<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
</form>
<script type="text/javascript">
function StartUpController($scope) {
    $scope.funding = { startingEstimate: 0 };
    $scope.computeNeeded = function() {
        $scope.needed = $scope.startingEstimate * 10;
    };
}
</script>

</body>

</html>

3 个答案:

答案 0 :(得分:0)

你犯了一个简单的错误:

$scope.computeNeeded = function() {
$scope.needed = $scope.funding.startingEstimate * 10;
};

{{funding.needed}}应为

{{needed}}

答案 1 :(得分:0)

您的视图显示funding.needed(基本上是$scope.funding.needed),但您的功能正在修改$scope.needed此外,您的输入已绑定到funding.startingEstimate,但您的功能是访问$scope.startingEstimate

您需要访问和修改正确的内容:

$scope.computeNeeded = function() {
    $scope.funding.needed = $scope.funding.startingEstimate * 10;
};

答案 2 :(得分:0)

嘿西米在这里为你工作小提琴,请查看演示here 这是我的HTML代码

<div ng-app>
 <form ng-controller="StartUpController">
 Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
 Recommendation: {{funding.needed}}
 </form>
</div>

和javascript代码

function StartUpController($scope) {
    $scope.funding = { startingEstimate: 0 };
    $scope.computeNeeded = function() {
        $scope.funding.needed = $scope.funding.startingEstimate * 10;
    };
}