How to get data typed with angular and save in an array of objects? [closed]

2

Using Angular, how could I do to get the data typed in a input and then save the data into an array of objects?

    
asked by anonymous 13.07.2017 / 22:28

2 answers

4

If the case is getting forms values the ideal method is this ...

var myApp = angular.module('myApp', []);

myApp.controller("testectrl", function($scope){
  
  $scope.gravar = [];
  
  $scope.adicionarDados = function(dados){
    $scope.gravar.push(dados);
  }
  
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><formname="gravaDados" ng-app="myApp" ng-controller="testectrl">

  <label>Dados1</label>
  <input type="text" ng-model="grava.dados1">
  <label>Dados2</label>
  <input type="text" ng-model="grava.dados2">
  <label>Dados3</label>
  <input type="text" ng-model="grava.dados3">
  <label>Dados4</label>
  <input type="text" ng-model="grava.dados4">
  
  <button ng-click="adicionarDados(grava)">
  gravar no objeto
  </button>
  
  {{gravar}}
  
</form>
    
13.07.2017 / 23:01
2

Would that be what you wanted?

angular.module("myApp", [])
    .controller("myCtrl", function($scope) {

  $scope.myClick = function() {
    let objeto = new Object();
    
    objeto.nome = $scope.Nome;
    objeto.email = $scope.Email;
    objeto.senha = $scope.Senha;
    
    console.log(objeto);
  };
  

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script><divng-app="myApp">
  <div ng-controller="myCtrl">
      
      <input type="text" placeholder="Nome" name="nome" autocorrect="off" ng-model="Nome">
      
      <input type="text" placeholder="E-mail" name="email" autocorrect="off" ng-model="Email">
      
      <input type="text" placeholder="Senha" name="senha" autocorrect="off" ng-model="Senha">
      
      <button class="button" ng-click="myClick()">Salvar</button>
  </div>
</div>
    
13.07.2017 / 22:54