3
Javascript:
var app = angular.module('app', []);
app.controller('controlador', function($scope, $http) {
$scope.user = {};
$scope.items = [];
var sum = 1;
$scope.submitForm = function() {
$http({
method : 'POST',
url : 'clone.php',
data : $scope.items,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
$scope.erroNome = data.errors.nome;
$scope.erroEmail = data.errors.email;
} else {
$scope.mensagem = data.mensagem;
}
});
};
$scope.addItem = function (user){
$scope.items.push({
nome: $("input[name='nome']").val(),
email: $("input[name='email']").val(),
soma: sum++
});
user.nome = '';
user.email = '';
};
});
HTML:
<body ng-app="app" ng-controller="controlador">
<form ng-submit="submitForm()">
<label>Nome: </label><input type="text" name="nome" ng-model="user.nome">
<span ng-show="erroNome">{{erroNome}}</span>
<label>E-mail: </label><input type="text" name="email" ng-model="user.email">
<span ng-show="erroEmail">{{erroEmail}}</span>
<input type="button" value="Adicionar" ng-click="addItem(user)" />
<input type="submit" value="Enviar" /><br /><br />
</form>
<br />
<div ng-repeat="item in items">
ID: {{item.soma}}<br />
Nome: {{item.nome}}<br /><label>Novo nome: </label><input type="text" name="nome"><br />
E-mail: {{item.email}}<br /><label>Novo e-mail: <input type="text" name="email"><br /><br />
<input type="button" value="Atualizar" />
<hr />
</div>
</body>
Utilize
$scope.$apply()
after the push.– OnoSendai