Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : How to inject Controller into Service?

I want to inject One Controller to Service.

I use AngularJs and Laravel and glup-ng-annotate.

/* DialogController*/
    (function(){
        "use strict";

        angular.module("music.controllers").controller('DialogController', function( $scope, $mdDialog ){
            $scope.hide = function() {
                $mdDialog.hide();
            };
            $scope.cancel = function() {
                $mdDialog.cancel();
            };
            $scope.answer = function(answer) {
                $mdDialog.hide(answer);
            };
        });
    })();

And this is Service

/* Service */
(function(){
    "use strict";

    angular.module("music.services").factory('DialogService', function( $mdDialog, DialogController){

        return {
            fromTemplate: function(template, $scope ) {

                var options = {
                    controller: DialogController,
                    templateUrl: template
                };

                if ( $scope ){
                    options.scope = $scope.$new();
                }

                return $mdDialog.show(options);
            },

            alert: function(title, content){
                $mdDialog.show(
                    $mdDialog.alert()
                    .title(title)
                    .content(content)
                    .ok('Ok')
                    );
            }
        };
    });
})();

I have this error

Error: [$injector:unpr] Unknown provider: DialogControllerProvider <- DialogController <- DialogService

like image 436
Boris Dehoumon Avatar asked Apr 24 '26 02:04

Boris Dehoumon


1 Answers

A service can be injected into a controller but the vice-versa is not possible. As the dependency injection in AngularJS supports injection of services in controllers.

like image 96
Prasheel Avatar answered Apr 27 '26 20:04

Prasheel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!