How to clean form with AngularJS?

3

I am creating an application with Ionic, I have a company registration and after registering the company I want to clear the form but I can not.

How to do this?

form

<form name="Empresa">
    <div class="list">
        <label class="item item-input" ng-class="{'has-errors':Empresa.nomeemp.$invalid, 'no-errors':Empresa.nomeemp.$valid}">
            <input type="text" placeholder="Nome" name="nomeemp" ng-model="Empresa.nome" ng-required="true">
        </label>
        <div class="error-container" ng-show="(Empresa.nomeemp.$dirty || !Empresa.nomeemp.$pristine)" ng-messages="Empresa.nomeemp.$error">
            <div ng-messages-include="templates/form-errors.html"></div>
        </div>
    </div>
    <button type="button" class="button button-block button-energized" ng-disabled="Empresa.$invalid" ng-click="addEmpresa(Empresa);">Cadastrar</button>
</form>

controller

$scope.addEmpresa = function(Empresa) {
    var _pathFile = document.getElementById("smallimage").src;
    var _fileName = _pathFile.substr(_pathFile.lastIndexOf('/') + 1);
    if(_fileName === 'no-image.png') {
        $ionicLoading.show({ template: APP_MESSAGES.escolhaImagem, noBackdrop: true, duration: 2000 });
    } else {
        $ionicLoading.show();
        EmpresaAPIService.addEmpresa(Empresa).then(function (data) {
            $ionicLoading.hide();
            var retorno = JSON.parse(data.response);
            if(retorno.status === 0){
                $ionicLoading.show({ template: retorno.msg, noBackdrop: true, duration: 2000 });
            }
            if(retorno.status === 1){  
                delete Empresa;
                Empresa.$setPristine();
                Empresa.$setUntouched(); 
                $ionicLoading.show({ template: retorno.msg, noBackdrop: true, duration: 2000 });
            }
        }, function (err) {
            $ionicLoading.hide();
        }, function (progress) {
            // PROGRESS HANDLING GOES HERE
        });
    }
}
    
asked by anonymous 06.01.2016 / 01:20

1 answer

3

When I use a form and need to reset it after use, clean and return to the original state, I use the $ scope.form definition, like this:

$scope.form = [];


$scope.addEmpresa = function(Empresa){  
//..resto do código
if(retorno.status === 1){  
    delete Empresa;           
    $scope.form.Empresa.$setPristine();
    $scope.form.Empresa.$setUntouched(); 
}
//..resto do código

And on your form, you should also change the name, thus:

<form name="form.Empresa">
    
06.01.2016 / 01:51