Angular create a new scope inside a service

I have a service that uses ngDialog (that the sole purpose of the service is to show general dialogs: warnings, confirmations, etc.). ngDialog requires that the object region object be passed as a parameter to interpolate the dialog template. Therefore, I need to create a scope, assign its properties and go to ngDialog.open. The problem is that I cannot inject $ rootScope (or $ scope) into the service and scope. $ New is the only way to find an empty area. When I enter $ rootScope, like this

myService.$inject = ['$rootScope', 'ngDialog']; 

I get the error: Unknown provider: $ rootScope Provider <- $ rootScope <- myService However this works if I just use the synak shortcut for decalring dependencies:

function myService ($ rootScope, ngDialog) {

// $ rootScope is available here

}

But this approach is not minimal. So the question is: how do I create a new area in a service?

UPDATE Here is a jsfiddle that shows the structure that I had in my project. The error occurred when the service was called from the directive, and now the problem has disappeared. jsfiddle

+5
source share
1 answer

Although you enter $rootScope and ngDialog , you still need to list them in your service.

 var myservice= function($rootScope, ngDialog) { // ... } myservice.$inject = ['$rootScope', 'ngDialog']; someModule.service('myservice', myservice); 

Update: this code works great

 var myservice = function($rootScope,ngDialog) { alert($rootScope); } angular.module('myapp',['ngDialog']).controller('datac',function($scope,myservice){ $scope.parties = []; myservice.$inject = ['$rootScope','ngDialog']; }); angular.module('myapp').service('myservice', myservice); 

Here is the plunker link: http://plnkr.co/edit/FXrE3jNhAYqtv7vVGzgx?p=preview

Update: Okay, sorry I injected the service into the controller instead of just injecting rootScope and ngDialog into the service outside the controller

 var myservice = function(obscope,obdialog) { alert(obscope); } myservice['$inject'] = ['$rootScope', 'ngDialog']; angular.module('myapp',['ngDialog']).controller('datac',function($scope,myservice){ $scope.parties = []; }); angular.module('myapp').service('myservice', myservice); 
+1
source

All Articles