Angular : How to inject Controller into Service?

标签 angularjs angularjs-scope

我想将一个 Controller 注入(inject)服务。

我使用 AngularJs 和 Laravel 以及 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);
            };
        });
    })();

这是服务

/* 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')
                    );
            }
        };
    });
})();

我有这个错误

错误:[$injector:unpr] 未知提供者:DialogControllerProvider <- DialogController <- DialogService

最佳答案

可以将服务注入(inject) Controller ,但反之则不行。由于 AngularJS 中的依赖注入(inject)支持在 Controller 中注入(inject)服务。

关于 Angular : How to inject Controller into Service?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36975010/

相关文章:

javascript - 如何将对象传递给指令?

javascript - 如何使用 underscore.js 将对象分配给 $rootscope?

angularjs - Intellij IDEA – 自动重新加载 Web 应用程序

jquery - 在 angularJS Controller 中使用 jQuery 的 $.ajax

javascript - 从指令 AngularJS 更改 Controller 范围值

angularjs - Angular 中的键值对存储和迭代

javascript - AngularJS - Controller 内的函数没有像我在简单示例中所期望的那样被调用

javascript - 使用 JSON 填充 js 数组时出现问题

javascript - AngularJS 设置本地存储(对象 json)

javascript - 如何以嵌套方式将预定义指令 (ng-click) 与自定义指令结合使用?