El Hombre Sin Nombre El Hombre Sin Nombre - 1 year ago 103
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;


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

Answer Source

set the property on the newUsers

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

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

$scope.newUsers = {};

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download