javascript - 变量不使用 ng-bind 呈现

标签 javascript angularjs

我正在开发书中的示例应用程序,AngularJS .

在下面的代码中,{{funding.needed}} 没有显示为 10 * startingEstimate。它按字面意思显示,即未呈现为页面上的 {{funding.needed}}

为什么?

<html ng-app>
<body ng-controller="TextController">

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">
      </script>

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

    <script>
        function StartUpController($scope) {
            $scope.funding = { startingEstimate: 0};

            $scope.computeNeeded = function() { 
                $scope.needed = $scope.startingEstimate * 10;
            };
        }
    </script>
</body>
</html>

最佳答案

您需要删除 TextController,因为您尚未定义它,并且它会在出错时阻止加载其他内容。此外,您还需要在某些地方规范您对 $scope.funding 对象的使用,您尝试仅使用没有父引用的成员。以下是一个工作版本(参见它在 http://plnkr.co/edit/Jz95UlOakKLJIqHvkXlp?p=preview 的 plunker 上工作)

<html ng-app>
<body>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">
      </script>

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

    <script>
        function StartUpController($scope) {
            $scope.funding = { startingEstimate: 0};

            $scope.computeNeeded = function() { 
                $scope.funding.needed = $scope.funding.startingEstimate * 10;
            };
        }
    </script>
</body>
</html>

关于javascript - 变量不使用 ng-bind 呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827303/

相关文章:

javascript - 忽略不关心顺序的正则表达式中的换行符

javascript - 尝试在 Alexa 中捕获贷款金额和利率并计算 EMI

javascript - 使用 jQuery 手动更改 radio 组时更改事件触发两次

javascript - React 调度未定义删除操作

javascript - 在 angularjs 中如何将 ngModel 对象传递给另一个 View ?

javascript - amChart heat 位置敏感 map

ios - ionic 范围无法点击ios

javascript - 如何在 Angular 1.5 组件中等待绑定(bind)(没有 $scope.$watch)

javascript - Angularjs 选项卡在渲染时加载微调器

javascript - AngularJS 过滤器 ng-repeat 不更新所有列