Problem when trying to get $Scope of form with Ionic

Asked

Viewed 659 times

0

I got the following form which sits within a modal:

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

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

$scope.usuarioForm.$setPristine();

But I get that mistake:

Error: $Scope.usuarioForm is Undefined

Another thing I noticed, when I try to clear the object like this: $scope.usuario = ""; after saving the data nothing happens either. IE, gives the impression that this modal is not inside the controller.

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

<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();
  };
  • how is the code of your controller?

  • I will edit the question and put the code of the save function that is in the controller

  • Could you also include the modal opening code? I think it will be important to define the problem.

  • I added in the question @Ricardorodriguesdefaria

  • @Techies dude, I couldn’t help you, it all seems okay, the form should have been assigned to Scope.

  • Yes, ta strange in Angular works, another thing that is not working is also when I try to grab rum object, for example: $scope.usuario.nome of error

Show 1 more comment

2 answers

0


I had this problem, I solved it this way:
- On your button you can pass the form this way: ng-click="save(user, user)".
- Exchange the Function call for $Scope.save = Function(user, form)
- At the time of the pristine you put form. $setPristine();
For me it solved, always reset the form that is working.

0

Your problem seems to be the modal creating a new Cope. The correct one would be to debug in 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();

Browser other questions tagged

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