How to clean input field with angular?

0

I'm trying to clear the text field after the submit, but I'm not getting it. Here are my codes

html: Here in html, I changed from ng-model="msg" to message.msg                                                                    Submit              

angular:

$scope.mensagem = {
    msg:""
  };
$scope.enviarMsg = function (mensagem) {
    var enviaMsg = {
        msg: mensagem,
        idUsuario: $window.localStorage.getItem('idUsuario'),
        idCep: $window.localStorage.getItem('idCep'),
        nome: $window.localStorage.getItem('nome')
    }

    $http.post("http://www.vigilantescomunitarios.com/www/php/enviaMsgLogra.php", enviaMsg).success(function (data){

        pegaMsgsLogra();
        $scope.mensagem = {
             msg:""
            };
    });
}

I get this message in the console:

  

TypeError: $ scope.msgForm. $ setPristine is not a function

    
asked by anonymous 10.02.2016 / 20:25

2 answers

1

Try this:

$scope.form = {}; //Definido logo no inicio do controller

$scope.form.msgForm.$setPristine(); //Definido no local onde deve ser feito a limpeza

And in your html:

<form name="form.msgForm">

This usually happens because your view is often switched or the form is initialized after controller . This way it keeps reference to the form.

To delete the input field, you must use the same reference of ng-model and NOT of the form name. In your case, it should look like this:

delete $scope.msg;

So you can remove all $scope.msgForm = []; settings from your controller .

    
10.02.2016 / 20:31
0
delete $scope.msg;

$scope.msgForm.$setPristine();

Try this out

    
10.02.2016 / 20:42