Reputation: 1197
This is my controller code. Show a popup and on button click, make some validations:
UZCampusWebMapApp.controller('PlanCtrl',function($scope, $ionicModal, $ionicLoading, $ionicPopup) {
$scope.confirmCreatePOI = function(data) {
var myPopup = $ionicPopup.show({
templateUrl: 'templates/pois/confirmCreatePOI.html',
title: 'Confirmar creación de POI',
scope: $scope,
buttons: [
{
text: '<b>Save</b>',
onTap: function() {
var invalidEmail = $scope.email.length==0 || $scope.email==null || typeof($scope.email)=='undefined';
if ($scope.emailChecked==true && invalidEmail) {
$ionicLoading.show({ template: 'Email is mandatory'});
}
else {
data.email = $scope.email;
$scope.finalSubmitCreatePOI(data);
}
}
},
{
text: 'Cancel'
}
]
});
};
});
This is the directive code where the previous controller function confirmCreatePOI
is called:
UZCampusWebMapApp.directive('formCreatePointOfInterest', function($ionicLoading) {
return {
restrict : 'A',
scope: true,
controller : function($scope) {
$scope.submit = function(data) {
console.log("Submit form createpoint of interest",data);
if($scope.createPOIform.$valid) {
$scope.confirmCreatePOI($scope.data);
} else {
$ionicLoading.show({ template: 'El formulario es inválido', duration: 1500})
}
}
}
}
});
And this is my templateUrl code:
<div id="confirm-create-poi-popup">
<p> Text </p>
<p> Text </p>
<div class="list">
<ion-checkbox ng-model="emailChecked">Receive notification</ion-checkbox>
<label class="item item-input">
<input type="email" ng-model="email">
</label>
</div>
</div>
So, after user have clicked on 'Save' button ( onTap event), I would like to check if email has been entered on input field.
But when I check it with the comprobation:
var invalidEmail = $scope.email.length==0 || $scope.email==null || typeof($scope.email)=='undefined';
The $scope.email.length==0
expression returns an error because email is undefined, so ng-model
property isn't working with the $scope
, I'm not getting any value on $scope.email
Why is that? Is the $ionicPopup $scope
property not working? Wrongly used?
Upvotes: 1
Views: 2742
Reputation: 11
I resolve this with this.scope.cd_ticket
:
var popup = $ionicPopup.show({
title: 'Validar Ticket',
scope: $scope,
template: '<input type="tel" placeholder="Digite o código do ticket" ng-model="cd_ticket" >',
buttons: [{
text: '<i class="icon ion-close"></i>',
type: 'popup-close'
},
{
text: '<i class="icon ion-checkmark"></i>',
type: 'common-btn',
onTap: function (e) {
return this.scope.cd_ticket;
}
}
]
}).then(function (res) {
if (res) {
$ionicPopup.alert({
title: 'Alerta',
template: 'Ticket: ' + $scope.cd_ticket + "<br>Resposta: " + res
});
}
console.log(res);
});
Upvotes: 1
Reputation: 1
I fixed that problem by moving scope declaration before templateUrl.
UZCampusWebMapApp.controller('PlanCtrl', function ($scope, $ionicModal, $ionicLoading, $ionicPopup) {
$scope.confirmCreatePOI = function (data) {
var myPopup = $ionicPopup.show({
scope: $scope,
templateUrl: 'templates/pois/confirmCreatePOI.html',
title: 'Confirmar creación de POI',
buttons: [{
text: '<b>Save</b>',
onTap: function () {
var invalidEmail = $scope.email.length == 0 || $scope.email == null || typeof ($scope.email) == 'undefined';
if ($scope.emailChecked == true && invalidEmail) {
$ionicLoading.show({
template: 'Email is mandatory'
});
} else {
data.email = $scope.email;
$scope.finalSubmitCreatePOI(data);
}
}
},
{
text: 'Cancel'
}
]
});
};
});
Upvotes: 0
Reputation: 1442
Please go through the code here: Ionic Play - Scope Issue in Ionic Popup.
I created email
and emailChecked
variables on a vm
object in your controller $scope
.
$scope.vm = {
email: '',
emailChecked: false
}
It is a good practice to pass variables encapsulated in an object, here vm
, instead of passing/using them as primitive types. vm
stands for View Model. Understanding Scopes.
Just a suggestion, you may show validation errors in a toast component, here is one that I am using ionicToast instead of using ionicLoading
.
Upvotes: 0
Reputation: 485
I found a few more places that you could possibly improve upon :)
Your onTap function should look like this:
onTap: function() {
// CORRECTION 1 - should use this.scope instead of $scope
var email = this.scope.email;
// CORRECTION 2 - check for 'undefined; first, then 'null', then 'length'
// (if the first OR condition is unsatisfied, the rest won't be checked for, saving time and a possible error)
var invalidEmail = typeof(email) == 'undefined' || email == null || email.length == 0 ;
// CORRECTION 3 - Either check for existence of 'emailChecked' or initialise it to 'false' in your templateUrl
var emailChecked = this.scope.emailChecked;
if (emailChecked == true && invalidEmail) {
$ionicLoading.show({ template: 'Email is mandatory' });
} else {
data.email = email;
$scope.finalSubmitCreatePOI(data);
}
}
Upvotes: 0