I'm using the ionic framework and need to be able to call a popup from muliple places in my code so I thought I would move it into a factory. The popup uses an input field and I want to get the value of it. Normally I would just call $scope.parentGate.answer
but because it's in a factory I don't have access to the scope. Any ideas how I can get the value of the input field?
Here's my code:
angular.module('starter.services', [])
.factory('parentGate', function ($ionicPopup, Helpers) {
return {
Show: function(scope, SuccessCallback) {
var first = Helpers.RandomNumber(1, 5) * 10;
var second = Helpers.RandomNumber(11, 22);
var title = 'What does ' + first + ' + ' + second + ' equal?'
// An elaborate, custom popup
return $ionicPopup.show({
template: '<h4 class="text-center">' + title + '</h4><div class="list"><label class="item item-input"><input type="number" ng-model="parentGate.answer" placeholder="Answer..."></label></div>',
title: "Parent Gate",
//subTitle: title,
scope: scope,
buttons: [
{ text: 'Cancel' },
{
text: 'Continue',
type: 'button-positive',
onTap: function(e) {
//
// I can't access $scope.parentGate.answer here.
// How else can I do it?
//
if ($scope.parentGate.answer == first + second) {
console.log("correct");
SuccessCallback();
} else {
console.log("wrong!");
e.preventDefault();
}
}
}
]
});
}
};
});