how do I clean fields with angular?

4

I want to be able to clear the fields after submitting and also apply the action at other times, for example when I click the "back" I used delete $scope.NomedaNgModel ; But it does not clean

    
asked by anonymous 14.07.2017 / 16:08

2 answers

3

You can clear the object as follows, so the ng-model will mirror it to view :

(function() {
  angular
    .module('MinhaApp', []);

  angular
    .module('MinhaApp')
    .controller('MeuController', MeuController);

  MeuController.$inject = [];

  function MeuController() {
    var vm = this;
    
    vm.limpar = _limpar;
    vm.objeto = {};

    _iniciar();

    function _iniciar() {
      vm.objeto.nome = 'TESTE';
      vm.objeto.tipo = 'TESTE 2';
    }

    function _limpar() {
      vm.objeto = {};
    }
  }
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="MinhaApp">
  <div ng-controller="MeuController as vm">
    <input type="text" ng-model="vm.objeto.nome" />
    <input type="text" ng-model="vm.objeto.tipo" />
    <button ng-click="vm.limpar()">Limpar</button>
  </div>
</div>
    
14.07.2017 / 16:17
3

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

myApp.controller("testectrl", function($scope){

  $scope.limpaCampos = function(dados){
  	delete $scope.teste;
    $scope.zeraCampos.$setPristine();
  } 
  
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><formng-app="myApp" ng-controller="testectrl" name="zeraCampos">
  
  <label>Campo1</label><input type="text" ng-model="teste.cp1">
  <label>Campo2</label><input type="text" ng-model="teste.cp2">
  <label>Campo3</label><input type="text" ng-model="teste.cp3">

  <button ng-click="limpaCampos(teste)">enviar</button>
  
</form>
    
14.07.2017 / 16:31