AngularJS示例代码不起作用 - 新手

时间:2013-05-03 21:35:44

标签: angularjs

我直接从一本书中复制了这段代码,而且我可以告诉它应该有用,但事实并非如此。我没有收到任何错误,但建议值只是显示角度字符串(用大括号),并且console.logs永远不会被命中。我哪里错了? (显然某处有拼写错误,但我不知道这是我的代码还是本书)。

<!DOCTYPE html>
<html>
    <head>
        <title>Calculator</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
    </head>
    <body>
        <form data-ng-controller="CalcController">
            Starting: <input data-ng-change="computeNeeded()" data-ng-model="funding.startingEstimate">
            Recommendation: {{funding.needed}}
        </form>
        <script>
            function CalcController($scope) {
                $scope.funding = {startingEstimate:0};
                computeNeeded = function() {
                    console.log("running");
                    $scope.funding.needed = $scope.funding.startingEstimate * 10;
                    console.log("funding needed: " + $scope.funding.needed);
                };
                $scope.$watch('funding.startingEstimate', computeNeeded);
            }
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

看起来这个地方缺少一个ng-app标签(我把它放在html上)。该指令告诉Angular将自己引导到页面上。

<html ng-app>

修改:ng-app的文档:http://docs.angularjs.org/api/ng.directive:ngApp