How to take angular typed data and save to an object array?

Asked

Viewed 1,768 times

2

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

2 answers

4

If the case is to take form 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>
<form name="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>

2

That’s the way you wanted it?

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>
<div  ng-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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.