Using ui.bootstrap.typeahead
, I select a product from my json list (by typing 'P'). My goal is to set the total price automatically. But I can't get the price of the selected product to display ( value="{{selected.price}}"
) so it can calculate the total. Not sure what I'm doing wrong.
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, status, headers, 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;
}
}
}]);
update #1
ng-model was changed from prc
to selected.price
<input id="price" name="price" ng-model="selected.price" ng-validate="number" type="text" value="{{selected.price}}">
This displays price and initial (1) quantity properly. Thanks @AbdelrhmanMohamed
Now the issue is that I'm not sure how to use the selected.price
value to calculate total price (price * quantity). When product is selected, price is displayed, but selected.price
is still undefined in displayPrice()
update #2
Problem: ng-change not firing when ng-model is changed
Solved by using $scope.$watch
:
$scope.$watch('selected.price', function() {
$scope.displayPrice();
});
i managed to leave out the $watch() function, you dont need it as all the bindings work ok.
Since ng-change is executed on each keydown event, we dont need it until we select an actual value from the list. Instead of ng-change
you can use typeahead-on-select
, which is executed as soon as a value is selected.
your typeahead directive:
<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">
apart from that there is not a major change on the code just minor changes: take a look on the live example: http://plnkr.co/edit/R3LON9?p=preview
Hope this helps, good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.