javascript – 使用Angularjs计算总价格和数量

使用ui.bootstrap.typeahead,我从我的json列表中选择一个产品(通过输入’P’).我的目标是自动设置总价.但是我无法显示所选产品的价格(value =“{{selected.price}}”),因此它可以计算总数.不知道我做错了什么.

Plunker:http://plnkr.co/edit/F9jPt7IZgsWyON2vEmic

<html>
<head>
  <link rel="stylesheet" href="style.css">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-animate.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.3/ui-bootstrap-tpls.min.js"></script>
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">

  <script src="script.js"></script>

  <script>
    angular.module("app",["ui.bootstrap.productautocomplete"]);
  </script>
</head>

<body>
  <div class="container" ng-app="app">

  <div ng-controller="ProductAutocompleteCtrl">

    <div class="col-sm-4">
      <input id="product_info" name="product_info" ng-change="displayPrice()" type="text" placeholder="Autocomplete - Type 'P' to find product's name" ng-model="selected" typeahead="product as product.name for product in products | filter:$viewValue | limitTo:8" class="form-control"
      autocomplete="off">
    </div>

    <div class="col-sm-2">
      <input id="price" name="price"  ng-model="prc"  ng-validate="number" placeholder="Autocomplete - Price" type="text" class="form-control" value="{{selected.price}}">
    </div>

    <div class="col-sm-1">
      <input id="quantity" name="quantity" ng-model="qty" ng-change="changePrice()" ng-validate="integer" placeholder="Quantity" type="text" class="form-control">
    </div>

    <div class="col-sm-2">
      <input id="total_prod1" name="total_prod1" ng-model="totalprod" ng-validate="number" placeholder="Total price" type="text" class="form-control" value="">
    </div>

  </div> <!-- ng-controller -->
 </div>
</body>
</html>

products.json

{"products":[
{"productversion":1,"name":"product1","price":"10.00"},{"productversion":2,"name":"product2","price":"20.00"}
]}

的script.js

angular.module('ui.bootstrap.productautocomplete',['ngAnimate','ui.bootstrap']);
angular.module('ui.bootstrap.productautocomplete').controller('ProductAutocompleteCtrl',['$scope','$http','$location',function($scope,$http,$location) {

  $scope.selected = undefined;

  var urlapiproducts = "products.json" ;
    //console.log(urlapiproducts);

    $http.get(urlapiproducts).success(function(data,status,headers,config) {
        $scope.products = data.products;  


    // console.log(data);
    }).error(function(data,config) {
        console.log("No data found..");
  });


    $scope.displayPrice = function(){
        var qty = 0;        
        var prc = 0;        

        qty = 1;
        prc = $scope.prc;    

        $scope.qty = qty;        

        $scope.totalprod = qty * prc;  
    }  

    $scope.changePrice = function(){
        var qty = 0;
        var prc = 0;

        qty = $scope.qty;       
        prc = $scope.prc;

        if(qty > 0 && prc > 0)
        {
            $scope.totalprod = qty * prc;
        }          
    }  

}]);

更新#1

ng-model从prc更改为selected.price

<input id="price" name="price" ng-model="selected.price"  ng-validate="number" type="text" value="{{selected.price}}">

这会正确显示价格和初始(1)数量.谢谢@AbdelrhmanMohamed

现在的问题是我不确定如何使用selected.price值来计算总价(价格*数量).选择产品时,会显示价格,但在displayPrice()中仍未定义selected.price

更新#2

问题:更改ng-model时,ng-change不会触发

使用$scope解决.$watch:

$scope.$watch('selected.price',function() {
  $scope.displayPrice();
});

解决方法

我设法省略了$watch()函数,你不需要它,因为所有绑定都可以正常工作.

首先将ng-change更改为typeahead-on-select

由于在每个keydown事件上执行ng-change,因此我们不需要它,直到我们从列表中选择实际值.您可以使用typeahead-on-select代替ng-change,它会在选择值后立即执行.

你的先行指令:

<input id="product_info" name="product_info" typeahead-on-select="displayPrice(product)" type="text" 
                      placeholder="Autocomplete - Type 'P' to find product's name" ng-model="product"
                      typeahead="product as product.name for product in products | filter:$viewValue | limitTo:8"
                      class="form-control" autocomplete="off">

除此之外,代码没有重大变化只是微小的变化:
看看实例:http://plnkr.co/edit/R3LON9?p=preview

希望这有帮助,祝你好运.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...