How to clear input field with angular?

Asked

Viewed 4,144 times

0

I’m trying to clear the text field after Ubmit, but I’m not getting it. Follow my own codes

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

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 on the console:

Typeerror: $Scope.msgForm. $setPristine is not a Function

  • If you take that: delete msgForm and this $scope.msgForm = [] doesn’t work?

  • No, it continues the same warning and nothing happens.

  • http://jsfiddle.net/AhGDC/243/

  • This appeared on the console Typeerror: Cannot read Property '$setPristine' of Undefined

  • I got it. I’m going to post here the solution

  • See if this post helps you https://answall.com/questions/94951/problema-ao-tenta-pega-o-scope-do-form-com-ionic/113532#113532

Show 1 more comment

2 answers

1

Try this way:

$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">

Usually this happens because often its view is switched or the form is initialized after the controller. So he keeps reference to the form.


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

delete $scope.msg;

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

  • Nothing, the text is still in the field.

  • And the console, which shows?

  • Nothing either. At least that kkkkkk

  • @Gustavosevero I edited my answer, take a look

  • Nothing yet... Very complicated.

  • Conseguiiiiiiii!!!!!! I’ll post here how it turned out... I had to change the name of ng-model

  • Did you not have a double statement under the name $Scope.msg?

  • @Gustavosevero your solution can solved, but the cleaning of your form is not complete. Don’t use $setPristine() don’t reset the status of your form, it will get some junk from the previous fill-in.

Show 3 more comments

0

delete $scope.msg;

$scope.msgForm.$setPristine();

Try this

  • $Scope.message has no definition and mensagem is just an obj js. This way, nothing will happen.

  • truth, I saw his form wrong. In case I would put in ng-model, ng-model="message.msg". And I think it works, but as in the case has only one input maybe it is not necessary

  • It was actually his reference that was wrong. He was trying to delete the form instead of the field. See my answer. = D

Browser other questions tagged

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