javascript - Get HTML element with AngularJS -
i have form has input text , select using selectize.js
. want access element entered in input , select via $scope , attribute ng-model, can't sent variables parameter function. when try access agencia
, nome
null. can me access elements?
html
<div class="form-group"> <h4>contatos</h4> <div ng-repeat="contato in contatos" class="form-group"> <label class="control-label col-sm-2">nome:</label> <div class="col-sm-10"> <input type="text" class="form-control" ng-model="contato.nome"> </div> <div class="form-group"> <h4>agência</h4> <div class="control-group"> <select ng-model="contato.agencia" ng-options="agencia.nome agencia in listaagencias" class="demo-default" selectize> </select> </div> </div> </div> <div align="right"> <button type="button" class="btn btn-xs btn-primary" ng-click="novocontato(contato.nome, contato.agencia)"> <span class="glyphicon glyphicon-plus"></span> </button> <button type="button" class="btn btn-xs btn-danger" ng-click="excluircontato()" ng-disabled="contatosvazio()"> <span class="glyphicon glyphicon-minus"></span> </button> </div> </div>
controller.js
$scope.novocontato = function (nome, agencia) { try{ //here have undefined undefined alert(nome + '\t' + agencia); }catch(err){ alert(err); } $scope.contatos.push({ nome: nome, agencia: agencia }) $scope.contatos.push({ nome: "", agencia: "" }); agenciasapi.getagencias().success(function (data) { var embedded = data._embedded; $scope.listaagencias = embedded.agencias; }).catch(function (error) { alert("erro ao obter listagem de agencias"); console.log(json.stringify(error)); }); };
actually don't know problem. can tell issues code
- i think got error
$scope.contatosis undefined
if yes, please declare empty $scope.contatos=[];
array in top of function
look
$scope.novocontato = function (nome, agencia) { $scope.contatos=[]; // code }
- why pass
nome, agencia
parameter function?
simply don't need pass these value function, because can values $scope.contato
object
look
$scope.novocontato = function () {// no parms $scope.contatos=[]; try{ alert($scope.contato.nome + '\t' + $scope.contato.agencia);// values $scope object }catch(err){ alert(err); } $scope.contatos.push({ nome: $scope.contato.nome, agencia: $scope.contato.agencia }); $scope.contatos.push({ nome: "", agencia: "" }); agenciasapi.getagencias().success(function (data) { var embedded = data._embedded; $scope.listaagencias = embedded.agencias; }).catch(function (error) { alert("erro ao obter listagem de agencias"); console.log(json.stringify(error)); }); };
- also
ng-click
shouldng-click="novocontato()"
Comments
Post a Comment