3

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

6
  • 1
    Factory / services can have their own controllers! You can't add/inject controllers to services / factory! Commented May 2, 2016 at 4:31
  • 5
    You're trying to put the cart in front of the horse. Commented May 2, 2016 at 4:32
  • Look this js code, Line 82 and 108, codepen.io/kyleledbetter/pen/gbQOaV Commented May 2, 2016 at 4:36
  • 1
    "look at this code" isn't a proper problem description. What exactly are you trying to do in words? Your concept is very backwards as others are all pointing out. See: How to Ask Commented May 2, 2016 at 4:43
  • 1
    Then your best bet is to do some translations the same way others do here Commented May 2, 2016 at 6:01

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

The controller should be injected by the $mdDialog service. Put quotes around the name, so that the $mdDialog service gets a string instead of a reference.

(function(){
    "use strict";

    angular.module("music.services")
      .factory('DialogService', function($mdDialog ̶,̶D̶i̶a̶l̶o̶g̶C̶o̶n̶t̶r̶o̶l̶l̶e̶r̶ ){

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

                var options = {
                    //REPLACE this
                    //controller: DialogController,
                    //WITH this
                    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')
                    );
            }
        };
    });
})();

In this case the options object is passed to the $mdDialog service which does the injection of the controller.

Under the hood, the $mdDialog service uses the $controller Service to inject the specified controller.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.