Javascript AngularJS检测已更改的输入
数据通过$http.get在我的AngularJS应用程序中检索并呈现如下:Javascript AngularJS检测已更改的输入,javascript,html,angularjs,Javascript,Html,Angularjs,数据通过$http.get在我的AngularJS应用程序中检索并呈现如下: # Angular app.controller('MainCtrl', function ($scope, $http) { $scope.cart = {}; $scope.cart.retrieve = function() { // make $http to retrieve data .success(function(results) {
# Angular
app.controller('MainCtrl', function ($scope, $http) {
$scope.cart = {};
$scope.cart.retrieve = function() {
// make $http to retrieve data
.success(function(results) {
$scope.cart.contents = results
});
};
$scope.cart.update = function(itemId) {
// make $http request to update cart
}
});
# HTML
<table ng-init="cart.retrieve()">
<tbody>
<tr ng-repeat="item in cart.contents">
<td>{{ item.price }}</td>
// item quantity input field
<td><input type="number" name="quantity" on-change="cart.update(item.id)"
ng-model="item.qty"></td>
<td>{{ item.price * item.qty }}</td>
</tr>
</tbody>
</table>
在输入字段中更改项目数量时,传递项目id的函数将激发:$scope.cart.update=functionitemId{…}。如何检索在更改事件中触发的此特定项的新数量值。我可以在函数中使用类似的东西吗?您可以将物品传递到购物车。更新函数:
on-change="cart.update(item)"
然后使用此项目的数量
$scope.cart.update = function(item) {
// do whatever you want with
// item.qty
}