I recently started using AngularJS and I'm trying to create an array and send it to the server using the register
function. Below is the Controller code snippet:
root.controller('mainController', function($scope) {
$scope.lineItems = [];
$scope.addItem = function (id) {
$scope.lineItems.push(id);
console.log($scope.lineItems);
// shows correct data -> Array [ "1", "2", "3", "4", "1" ]
};
$scope.register = function () {
// send data to server
console.log($scope.lineItems);
// displays an empty array -> Array []
};
});
Here are the HTML code snippets and directives used:
<div class="container" ng-controller="mainController">
<a ng-click="register()"></a>
<div ng-repeat="product in products">
<a class="btn" ng-click="addItem(product.id)">Add</a>
</div>
</div>
The issue arises when I try to invoke the register
function, it returns an empty array instead of the array elements present, unlike what happens with the addItem
function.