El Hombre Sin Nombre El Hombre Sin Nombre - 2 months ago 17
AngularJS Question

Angular ID autoincremental in controller

I´m working on a user CRUD with Angular. I can delete, read and update all users but when I want save i have one problem: The users have a increment ID.

I use 'file inputs' to add new email and user, but I want add increment ID.

I have this code, but can´t add 'newID' in push $scope.

$scope.users = [];
$scope.newUsers = [];

$scope.addUser = function() {
var newId = $scope.users.length + 1;
$scope.users.push($scope.newUsers);
};


In HTML

<form ng-submit="addUser()">
<input type="text" ng-model="newUsers.name" required>
<input type="email" ng-model="newUsers.email" required>
<button>Agregar</button>
</form>

M B M B
Answer

set the property on the newUsers

$scope.addUser = function() {
   $scope.newUsers.ID = $scope.users.length + 1;
    $scope.users.push($scope.newUsers);
};

Also, newUsers should be an object(with {}) not an array(with [])

$scope.newUsers = {};