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();
};