Issue
I have an array which is:
$scope.products=[
{name:'Orange', price:'1.15'},
{name:'Apple', price:'1.08'},
{name:'Pear', price:'1.85'},
{name:'Mango', price:'2.10'},
{name:'Blueberry', price:'0.55'},
{name:'Strawberry', price:'1.40'}
];
And I've added a button to every array, since I am creating a shopping cart. I created an empty array which is the cart of the selected products, but when I try to add these objects to my new array it doesnt work. The code is the following:
<li ng-repeat="x in products | orderBy:'name'">
{{x.name + " : " + '$' + x.price}}
<button type="button" class="btn btn-dark" ng-click="addItem(x)">
Add product
</button>
</li>
and the controller is:
$scope.cart=[];
$scope.addItem = function(x){
$scope.cart.push($scope.x);
console.log($scope.cart);
};
Solution
You are passing the object into the function in the ng-click
as x
and it is not $scope.x
so just change:
$scope.cart.push($scope.x);
To
$scope.cart.push(x);
Answered By - charlietfl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.