Using AngularStrap modal from service
I can not get Angular-Strap modal to work from a service...
This is necessary because some actions and logic the trigger a modal do not reside within the controller of the page that is displayed...they are also in services. So is there a way to wrap AngularStrap modals in a service and use it from the service function since it seems so heavily dependent on the $scope to operate?
I don't know why you want to do it that way but I think you can inject $rootScope into your service and then create an isolated scope from it ($rootScope) by calling $rootScope.$new(true). After this, you have a new scope that you can pass to the $modal service.
Try this: service.js
(function () {
var app = angular.module('services', ['modal-controllers']);
var mymodalservice= function ($modal, $q, $factory) {
let self = this;
self.$modal = {};
self.$modal.detailOS = function (year, num) {
var modalDetailOS = $modal(
{
controller: 'modalDetailOSController as m',
templateUrl: 'views\\modal\\detailOS.html',
show: false,
resolve : {
os : function() { //YOU CAN USE FACTORY HERE
var defer = $q.defer();
defer.resolve(
{{year: year,
num: num
});
return defer.promise;
}}
}
);
modalDetailOS.$promise.then(modalDetailOS.show);
};
}
app.service('mymodalservice',mymodalservice);
})();
modal-controller.js
(function () {
var app = angular.module('modal-controllers', []);
app.controller('modalDetailOSController', function (os) { //os from resolve
var vm = this;
vm.doc = os.year + '/' + os.num;
vm.searchAnother = function () {
//.......
}
});
})
anyControlle.js:
function myController(mymodalservice) {
let vm = this;
vm.detailOS = function () {
mymodalservice.$modal.detailOS(2017,1);
}
vm.detail2 = mymodalservice.$modal.detailOS;
}
I was able to use the suggestions to complete my task. Thank you!!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.