I know the same kind of question has been asked on SO many times, but I couldn't find the exact question that matches my requirement. So here's my question:
I have a function in my controller:
showPNG = function() {
var pngPromise = $modal({
template: 'index.html',
persist: true,
show: false,
backdrop: 'static',
scope: $scope,
modalClass: 'preview-png'
});
$q.when(pngPromise).then(
function(pngElement) {
pngElement.modal('show');
}
);
};
I have 3 controllers having the same function. So I'm trying to refactor it in such a way that it is written in some service and can be called from all the controllers. What I've done so far is:
In Service:
var service = module.exports = function ($modal, $q) {
return {
showPNG : function(scope) {
var pngPromise = $modal({
template: 'index.html',
persist: true,
show: false,
backdrop: 'static',
scope: scope,
modalClass: 'preview-png'
});
$q.when(pngPromise).then(
function(pngElement) {
pngElement.modal('show');
}
);
}
};
};
service.$inject = ['$modal', '$q'];
In Controller:
...
myService.showPNG($scope);
...
This code works without any error. But the question is, can passing $scope
as an argument to a function is service cause any side effects? Is there any better alternate to this approach?
Thanks.