5
votes

I have a service that uses ngDialog (that's the only purpose of the service - to show common dialogs: alerts, confirms, etc). ngDialog requires scope object to be passed as parameter to interpolate dialog's template. So I need to create a scope, assign it's properties and pass to ngDialog.open. The problem is I can't inject $rootScope (or $scope) into service, and scope.$new is the only way I could find to create an empty scope. When I inject $rootScope like this

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

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

function myService($rootScope, ngDialog) {

//$rootScope here is accessible

}

But this approach is not minification-safe. So the question is: how do I create new scope in a service?

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

1
ngDialog requires scope object to be passed as parameter - I don't think this is correct. Did you try not passing a scope?pablochan
it requires scope to interpolate template. If I don't pass scope, dialog will show up, but I can't change anything inside of it without scopeVitaly

1 Answers

1
votes

Although you inject the $rootScope and ngDialog you still need to specify them in your service.

var myservice= function($rootScope, ngDialog) {
  // ...
}

myservice.$inject = ['$rootScope', 'ngDialog'];
someModule.service('myservice', myservice);

Update : This code perfectly works

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 plunker link : http://plnkr.co/edit/FXrE3jNhAYqtv7vVGzgx?p=preview

Update : Ok sorry i was injecting the service in controller instead just inject the rootScope and ngDialog in 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);