You DON"T. You use a service inside a controller by injecting it.
Accessing controllers from services (through whatever sneaky means) flies
against a clean MVC architecture and what 'services' are intended to do.
On Sunday, March 8, 2015 at 3:50:03 PM UTC-4, pierre taillard wrote:
>
> Hello,
>
> I have a question about how to use controller inside services.
> I think a example can be more useful to understand my trouble:
>
> I have defined a service called myService which show a material design
> dialog. And my mdDialog have a controller call 'myController'.
> But the only way i found to define my controller is like below:
> *.service('myService', function($mdDialog) {*
> * this.edit = function(ev) {*
> * $mdDialog.show({*
> * controller: myController,*
> * templateUrl: 'templates/dialog.html',*
> * targetEvent: ev*
> * });*
> * };*
> * });*
>
> *function myController($scope, $mdDialog) {*
>
> * $scope.cancel = function() {*
> * $mdDialog.cancel();*
> * };*
>
> * $scope.ok = function() {*
> * $mdDialog.hide();*
> * }*
> *};*
>
> I try to found a way to define my controller like this:
> *.service('myService', function($mdDialog) {*
> * this.edit = function(ev) {*
> * $mdDialog.show({*
> * controller: myController,*
> * templateUrl: 'templates/dialog.html',*
> * targetEvent: ev*
> * });*
> * };*
> * }).controller('myController', function($scope, $mdDialog) {*
>
> * $scope.cancel = function() {*
> * $mdDialog.cancel();*
> * };*
>
> * $scope.ok = function() {*
> * $mdDialog.hide();*
> * }*
> *});*
>
> But i have this error: Unknown provider: myServiceProvider <- myService
> <- myMainController
>
> *Does anyone have a idea to solve this issue?*
>
> Thanks,
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.