The source of the problem is that $ionicModal
always always creates a% new% co based on your options when you run
$ionicModal.fromTemplateUrl(template, {
scope: ...
animation: ...
focusFirstInput: ...
backdropClickToClose: ...
hardwareBackButtonClose: ...
});
If the $scope
option is present, the modal will create a child of that scope
otherwise it will create a child of $scope
. In one way or another the bindings in the modal template are never established in the $rootScope
that you manipulate but in a child of this.
This causes you to fall into a problem known as the dot notation which basically causes that instead of modifying the properties of your $scope
you modify those of your child by so when you try to get your values waiting for them to be present they are always undefined.
You must remember that the $scope
objects are pure javascript objects and that the prototypical javascript inheritance works on them too since angular always defines an $scope
object from which to inherit except for $scope
whose parent is null ( and only inherits from Object ). The $ scope. $ New () method is the one used internally to make the said inheritance work.
In the following snippet I have two controllers one inside the other which creates two $rootScope
that inherit each other and the binding is shown in both the father and the son. Both get the value well but when you change it only changes the child's since what happens is that creates a new property instead of updating the property of the parent.
angular.module('app', [])
.controller('PadreCtrl', function($scope) {
$scope.valor = '10';
})
.controller('HijoCtrl', function($scope) {});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="PadreCtrl">
Padre {{valor}}
<hr>
<div ng-controller="HijoCtrl">
Hijo {{valor}}
<hr>
<input ng-model="valor">
</div>
</div>
You can read a little more about the subject in the question
$ scope vs. this in angularjs
The solution
Create a new property in your $scopes
and use this property to manipulate the values.
Why does this work?
Well because when you try to assign
$scope.propiedad = valor;
The inheritance chain is not consulted because what you are doing is creating a new property. If you execute instead
$scope.objeto.propiedad = valor;
In this case the javascript will try to find a property called $scope
within the objeto
and when not found it will start looking for it in its parent, in the father of this and so on until it finds it by consulting the string inheritance completes as long as it does not find that property.
Do not forget to initialize the object
$scope.objeto = {};
The $scope
of your controller has this property and this is used to assign the values giving you the result you expect.
Here I leave you a plunker and a working demo
link
angular.module('calendarioEstu', ['ionic'])
.controller('tareasEstu', tareasEstu);
tareasEstu.$inject = ['$scope', '$ionicModal'];
function tareasEstu($scope, $ionicModal) {
// Esta es la propiedad a usar
$scope.nueva = {};
$scope.modal = $ionicModal.fromTemplate('<ion-modal-view>' +
'<ion-header-bar class="bar bar-header bar-balanced">' +
'<h1 class="title">Nueva Tarea</h1>' +
'<button class="button button-clear button-primary" ng-click="modal.hide()">Cerrar</button>' +
'</ion-header-bar>' +
'<ion-content>' +
'<div class="list">' +
'<label class="item item-input">' +
'<span class="input-label">Tarea</span>' +
'<input placeholder="Tarea" type="text" ng-model="nueva.tareatxt">' +
'</label>' +
'<label class="item item-input">' +
'<span class="input-label">Fecha</span>' +
'<input type="date" ng-model="nueva.fechatxt">' +
'</label>' +
'<button class="button button-block button-balanced" ng-click="agregarTareas()">Crear</button>' +
'</div>' +
'</ion-content>' +
'</ion-modal-view>', {
scope: $scope
});
$scope.agregarTareas = function() {
var datosTarea, fechaDigitada;
datosTarea = {
Tarea: $scope.nueva.tareatxt,
Fecha: $scope.nueva.fechatxt
};
console.log(datosTarea.Tarea + " - " + datosTarea.Fecha);
if (typeof datosTarea.Tarea === 'undefined' || typeof datosTarea.Fecha === 'undefined') {
alert("Deben estar ambos campos llenos");
} else {
fechaDigitada = moment(new Date(datosTarea.Fecha)).format('YYYY-MM-DD');
alert(fechaDigitada + " - " + datosTarea.Tarea);
}
}
}
<script src="http://code.ionicframework.com/1.3.1/js/ionic.bundle.min.js"></script>
<link href="http://code.ionicframework.com/1.3.1/css/ionic.min.css" rel="stylesheet">
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<div ng-app="calendarioEstu" ng-controller="tareasEstu">
<ion-header-bar class="bar-balanced" align-title="center">
<a href="#/menuestu" class="button button-icon icon ion-ios-arrow-back"></a>
<p class="title">Calendario</p>
<button class="button button-icon ion-ios-plus-outline" ng-click="modal.show()"></button>
</ion-header-bar>
<ion-view>
<ion-content has-header="true">
<div class="list">
<div class="item item-divider">
Tareas
</div>
</div>
</ion-content>
</ion-view>
</div>