javascript - 如何使用angularjs获取html元素内的文本?

标签 javascript html angularjs

如标题所述,如何使用 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/

相关文章:

javascript - 仅当分配给新变量时闭包才起作用 - Javascript

javascript - 更改网页中的鼠标指针

javascript - 以像素为单位的字符大小

javascript - 如何限制字符串不允许数字或特殊字符?

javascript - 遵循 Google 文档后,无法从回调函数访问 $scope

javascript - ng-click 在第一次点击后停止工作

javascript - javascript中这个条件是什么意思?

javascript - 在 Chrome 和 FF 中尝试通过另一个 Angular 应用程序获取一个 Angular 应用程序的根范围时未定义

javascript - 循环 AngularJS $http 调用 - TypeError : object is not a function

javascript - 如果我的 javascript 变量在 mvc3 中的值中有 at 符号怎么办?