1

I am trying to add objects to $scope.orderitems and if the object is already in the $scope.orderitems I want to change the quantity property of the object using angular.forEach instead of adding another object. Whenever I call the additem function I get an error that orderitems is not defined.

Here is my code

HTML

<input type="text" ng-model="item.name">
<button type="button" ng-click="additem(item)">

<ul>
    <li ng-repeat="orderitem in orderitems">
        <p>{{ orderitem.name }} | {{ orderitem.qty }}</p>
    </li>
</ul>

JS

app.controller('OrderCtrl', function($scope) {
    $scope.orderitems = [];
    $scope.additem = function(data) {

        angular.forEach(orderitems, function(orderitem) {
            if (orderitem.id === data.id) {
                orderitem.qty = orderitem.qty++;
            } else {
                data.qty = 1;
                $scope.orderitems.push(data);
            }
        });
    };
});
1
  • 1
    angular.forEach($scope.orderitems,function(orderItem...... Commented Dec 17, 2013 at 20:18

2 Answers 2

7

Here's a working fiddle: http://jsfiddle.net/rodhartzell/hbN4G/

HTML

<body ng-app="app">
    <div ng-controller="OrderCtrl">
    <input type="text" ng-model="item.name">
        <button type="button" ng-click="additem()">add</button>

    <ul>
        <li ng-repeat="orderitem in orderitems">
            <p>{{ orderitem.name }} | {{ orderitem.qty }}</p>
        </li>
    </ul>
</div>

CONTROLLER

var app = angular.module('app', []);

app.controller('OrderCtrl', function($scope) {

    $scope.orderitems = [{name: 'foo', id:1, qty:1}];
    $scope.additem = function() {
        var exists = false;
        var data = {
            id : $scope.orderitems.length + 1,
            name: $scope.item.name,
            qty: 1
        }
        angular.forEach($scope.orderitems, function (item) {
            if (item.name == data.name) {
                exists = true;
                item.qty++;
                return false;
            }
        });
        if(!exists){
            $scope.orderitems.push(data);
        }

    };
});
Sign up to request clarification or add additional context in comments.

1 Comment

I updated the fiddle -> Here's a working fiddle: jsfiddle.net/rodhartzell/hbN4G/2 . Just in case there is anyone out there still using Angular 1.
0
$scope.orderitems = [];
$scope.additem = function(data) {
    var item = null
    angular.forEach(orderitems, function(orderItem) {
        if (orderItem.id === data.id) {
            item = orderItem
            return false;
        }
    });
    if (item == null) {
       item = {
         id: data.id, qty : 0
       }
       $scope.orderitems.push(item)
    }

    item.qty++
};

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.