如标题所述,如何使用 angularjs 获取 div 中的文本?即:
<div class='container' ng-app=''>
<input type='text' ng-model='qty'>
<div class='price'></div>
<div class='total'>{{qty * price}}</div>
</div>
输入是可编辑,可以填入任意数字,而价格是固定,不可编辑,但每件商品都不一样,现在如何得到使用 AngularJS 将价格的内容与输入的数量相乘并在总 div 中显示总计?
最佳答案
您需要在 Controller 中初始化
值。
function MyCtrl($scope) {
$scope.price= $('.price').text();
$scope.qty=0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="MyCtrl">
<input type="text" ng-model='qty'>
<div class="price">3.4</div>
<div class='total'>Total: {{ qty * price }}</div>
</div>
</div>
关于javascript - 如何使用angularjs获取html元素内的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674230/