Well, I'm having the following problem, i have a model windows that i can access form a controller, the problem is i need it to be accessible from more than one controller, so i thought to myself, "maybe i can create factory that i can inject into my controller and call the modal from there?" and so i tried the following :
.factory('FSTestService', function ($rootScope, $ionicModal) {
var completed = false;
var loggedIn = false;
// Create the ILS questionnaire modal that we will use later
$ionicModal.fromTemplateUrl('templates/FS-Form-container.html', {
scope: $rootScope
}).then(function (modal) {
$rootScope.FSModal = modal;
});
return {
FSFrom: function () {
$rootScope.FSModal.show();
}
}
})
and then on the controller i tried:
.controller('CursosCtrl', function ($scope, CursosService, FSTestService) {
FSTestService.FSForm;
})
But nothing happens, and if i go and call the "FSForm" as a function, that is to say change the aforementioned code as follows:
.controller('CursosCtrl', function ($scope, CursosService, FSTestService) {
FSTestService.FSForm();
})
I just get a bunch of error everywhere, so my question is, is this even possible ? what would be the standard way to proceed ?.
I am currently looking into the same problem and found this solution. I know I'm pretty late on this, but better late than never!
angular.module('evenementoApp', ['ionic'])
.service('ModalService', function($ionicModal, $rootScope) {
var init = function(tpl, $scope) {
var promise;
$scope = $scope || $rootScope.$new();
promise = $ionicModal.fromTemplateUrl(tpl, {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
return modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
return promise;
}
return {
init: init
}
})
.controller('MainCtrl', function($scope, ModalService) {
$scope.modal1 = function() {
ModalService
.init('modal1.html', $scope)
.then(function(modal) {
modal.show();
});
};
$scope.modal2 = function() {
ModalService
.init('modal2.html')
.then(function(modal) {
modal.show();
});
};
})
It's then just a matter of calling $scope.modal1() or $scope.modal2() from your markup.
Based on this question and other needs I create a service that can be useful.
See this post: Ionic modal service
or see in operation: CodePen
(function () {
'use strict';
var serviceId = 'appModalService';
angular.module('app').factory(serviceId, [
'$ionicModal', '$rootScope', '$q', '$injector', '$controller', appModalService
]);
function appModalService($ionicModal, $rootScope, $q, $injector, $controller) {
return {
show: show
}
function show(templateUrl, controller, parameters) {
// Grab the injector and create a new scope
var deferred = $q.defer(),
ctrlInstance,
modalScope = $rootScope.$new(),
thisScopeId = modalScope.$id;
$ionicModal.fromTemplateUrl(templateUrl, {
scope: modalScope,
animation: 'slide-in-up'
}).then(function (modal) {
modalScope.modal = modal;
modalScope.openModal = function () {
modalScope.modal.show();
};
modalScope.closeModal = function (result) {
deferred.resolve(result);
modalScope.modal.hide();
};
modalScope.$on('modal.hidden', function (thisModal) {
if (thisModal.currentScope) {
var modalScopeId = thisModal.currentScope.$id;
if (thisScopeId === modalScopeId) {
deferred.resolve(null);
_cleanup(thisModal.currentScope);
}
}
});
// Invoke the controller
var locals = { '$scope': modalScope, 'parameters': parameters };
var ctrlEval = _evalController(controller);
ctrlInstance = $controller(controller, locals);
if (ctrlEval.isControllerAs) {
ctrlInstance.openModal = modalScope.openModal;
ctrlInstance.closeModal = modalScope.closeModal;
}
modalScope.modal.show();
}, function (err) {
deferred.reject(err);
});
return deferred.promise;
}
function _cleanup(scope) {
scope.$destroy();
if (scope.modal) {
scope.modal.remove();
}
}
function _evalController(ctrlName) {
var result = {
isControllerAs: false,
controllerName: '',
propName: ''
};
var fragments = (ctrlName || '').trim().split(/\s+/);
result.isControllerAs = fragments.length === 3 && (fragments[1] || '').toLowerCase() === 'as';
if (result.isControllerAs) {
result.controllerName = fragments[0];
result.propName = fragments[2];
} else {
result.controllerName = ctrlName;
}
return result;
}
} // end
})();