简体   繁体   中英

AngularJS detect which input has been changed

Data is retrieved in my AngularJS app via $http.get and rendered like this:

# 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>

When the item quantity is changed in input field, the function that passes item id fires: $scope.cart.update = function(itemId) { ... } . How do I retrieve this new quanity value for this particular item that triggered on-change event. Is there something in angular like this I can use inside the function?

You can pass item to your cart.update function:

on-change="cart.update(item)"

And then use the qty of this item

$scope.cart.update = function(item) {
    // do whatever you want with
    // item.qty
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM