4

I got problem, I'm trying to get controller instance in my service:

myAppModule.service('modalService',
function ($modal, $controller) {
   var controller = $controller('ExperienceDetailsModalCtrl');
});

but I've got error: TypeError: Cannot read property '$scope' of undefined

Is it possible to access controller (defined in another file) and pass it to modal?

My controller:

myAppIndexModule
.controller('ExperienceDetailsModalCtrl', function ($scope) {

});
2
  • 1
    Why do you want to access the controller inside the service?? generally, it's one way the controller uses the service and that's it, that's the goal of the service is to provide a service to the controllers Commented May 2, 2015 at 22:23
  • I would like to define all my modals in one place - I want to avoid passing a controller name and template each time, when I want to open a modal. So I supposed that create a service and functions for modals is a good idea. That's way. Maybe someone will propose me a better approach to do that? Commented May 3, 2015 at 10:23

1 Answer 1

4

You can't access controller scope in service, factory or provider. The data which you wanted to share should be placed inside a service. & make it available to other controller.

I think you do want to pass controller scope to $modal then you can achieve this by doing from controller itself.

$modal.open({$scope: $controller('ExperienceDetailsModalCtrl', {$scope: $scope}), templateUrl: 'abc.html'})

Update

You could do it like below

myAppModule.service('modalService',
function ($modal, $controller, $rootScope) {
   var scope = $rootScope.$new(true);
   $controller('ExperienceDetailsModalCtrl',{scope: $scope });
   //in scope now you will have ExperienceDetailsModalCtrl scope
});
Sign up to request clarification or add additional context in comments.

4 Comments

That works fine. But I would like to do that in different way. I would like to define all my modals in one place - I want to avoid passing a controller name and template each time, when I want to open a modal. So I supposed that create a service and functions for modals is a good idea. Could you give me a advice, how to build it?
@Theo try my updated answer, its just suggestion may not work
Great! It works (almost :). When I define my controller it that way: controller('ExperienceDetailsModalCtrl', function () . But when I'm defining it like that: controller('ExperienceDetailsModalCtrl', function ($scope) - [with $scope] i got an exception: Unknown provider: $scopeProvider <- $scope.
But could you read comment above? It almost works - I updated my question - added controller code. When I try to inject $scope it doesn't work - exception: Unknown provider: $scopeProvider <- $scope. Without injecting scope it works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.