2

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?

1 Answer 1

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.