angular-strap icon indicating copy to clipboard operation
angular-strap copied to clipboard

Using AngularStrap modal from service

Open RobertVogt opened this issue 9 years ago • 4 comments

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?

RobertVogt avatar Sep 06 '17 14:09 RobertVogt

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.

cossay avatar Sep 14 '17 10:09 cossay

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;
}

augustoeliezer avatar Oct 03 '17 01:10 augustoeliezer

I was able to use the suggestions to complete my task. Thank you!!

RobertVogt avatar Mar 28 '18 13:03 RobertVogt

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.

lock[bot] avatar Jan 25 '19 19:01 lock[bot]