1
I have a problem with my application.
When I use the ng-repeat
of angularJS
to create a table, and in this table there is a input
, and in that field input
, comes filled with a value.
There is also a SAVE button, where it takes the value of INPUT
and sends as parameter.
That’s the problem. If I don’t click on Input
then click on save, it does not send any value.
Someone has an idea ?
HTML
<body ng-app='app-pedido'>
<div ng-controller='ctl-pedido'>
<table class='table table-bordered table-striped'>
<thead>
<tr>
<th>Código</th>
<th>Descrição</th>
<th>Quantidade</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr ng-repeat='ax in itens'>
<td>{{ax.codigo}}</td>
<td>{{ax.descricao}}</td>
<td><input class='form-control' type='number' ng-model='txtQuantidade' ng-value='ax.quantidade'></td>
<td><button ng-click='Salvar(ax.codigo, txtQuantidade)' type='button' class='btn btn-primary btn-block'>Salvar</button></td>
</tr>
</tbody>
</table>
</div>
</body>
JAVASCRIPT
angular.module('app-pedido', [])
.controller('ctl-pedido', function($scope, $http) {
$scope.itens = [{
codigo: '00002305',
descricao: 'Módulo de Memória',
quantidade: '10.00'
}];
$scope.Salvar = function(codigo, quantidade) {
alert('O código do produto é ' + codigo + ' e a quantidade é ' + quantidade);
};
});
You are using a form with post or are sent the data via json?
– adelmo00
Sending data via JSON. The problem is exactly what is happening in jsfiddle. Value is only passed in the parameter if I "enter" the input. There it works.
– Guilherme Catini
Change the input type to text, I removed ng-value, put ng-model referencing the quantity in Ax: https://jsfiddle.net/1v0v5ns2/13/
– adelmo00
Perfect! You could explain why type number does not work?
– Guilherme Catini
I’ll look it up and put it in as an answer. It has a more detail, you can edit the quantity, and pass the whole list as json that the angular passes the updated values. I have a similar list on a system, where the user can edit the amount of multiple items.
– adelmo00
I’ll try to do it the way you told me. I am beginner with Angularjs, I have a little difficulty, but your idea is in case to change in batch, all items at once. That’s right?
– Guilherme Catini
No, you can have a lot and want to change only one.
– adelmo00
@Guilhermecatini the
type=number
works... if you give it anumber
: https://jsfiddle.net/1v0v5ns2/21/– MoshMage
Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!
– Sorack