Problem trying to get $ form scope with Ionic

0

I have the following form that is inside a modal:

<form name="usuarioForm"  novalidate="">

At the end of this form I have a button where I pass the object that was filled to a function, and in that function I try to clear the form this way:

$scope.usuarioForm.$setPristine();

But I get this error:

  

Error: $ scope.userForm is undefined

Another thing I noticed, when I try to clear the object this way: $scope.usuario = ""; after saving the data nothing happens as well. That is, it gives the impression that this mode is not within the controller.

I put this modal inside my page that has the controller: loginCtrl like this:

<script id="my-modal.html" type="text/ng-template">
   <ion-modal-view class="back">
   <ion-header-bar class="bar-dark">
       <h1 class="title">Novo Usuário</h1>
   </ion-header-bar>
   <ion-content >
   <form name="usuarioForm"  novalidate="">
   ...//Todo código da página

   <button class="button button-block button-positive" 
        type="submit" ng-click="salvar(usuario)"      
        ng-disabled="usuarioForm.$invalid">
        <i class="ion-checkmark"></i>
        Cadastrar
   </button>
   </form>
   </ion-content>
   </ion-modal-view>
</script>

Controller:

  $scope.salvar = function(usuario) {
    usuarioAPI.saveUsuario(usuario).success(function(data) {
      console.log("Salvar!");
      delete $scope.usuario;
      $scope.usuarioForm.$setPristine();
      $scope.closeModal();


    })
    .error(function(response, status) {
      console.log("erro " + status);

    });

  }

Modal opening code:

$ionicModal.fromTemplateUrl('my-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });

  $scope.openModal = function() {
    $scope.modal.show();
  };

  $scope.closeModal = function() {
    $scope.modal.hide();
  };
    
asked by anonymous 27.10.2015 / 19:11

2 answers

0

I had this problem, I decided as follows:
 - On your button you can pass the form this way: ng-click="save (user, userForm)" . Home  - Change function call by $ scope.salvar = function (user, form)
 - At pristine time you put form. $ SetPristine () ; Home For me it solved, always resets the form that is working.

    
16.02.2016 / 15:29
0

Your problem seems to be the modal creating a new scope. The correct one would be debug in the chrome, set breakpoints and analyze the variable $ scope and such.

Since you set the modal scope, you can try to access the parent scope like this:

$scope.$parent.usuarioForm.$setPristine();
    
24.11.2015 / 14:53