将$ mdDialog注入工厂会创建循环依赖

时间:2017-03-17 08:14:39

标签: angularjs angular-material

enter image description here我开始将角度材料包含在角度1.5v项目中。我也在使用ionic-latest,其中包括捆绑的ng-animate和ng-sanitize文件。当我向工厂注入$ mdDialog服务时,我得到以下错误但是当我将$ mdDialog注入控制器时它工作正常。可能是什么原因如何解决这个问题?

Uncaught Error: [$injector:cdep] Circular dependency found: $http <- $templateRequest <- $$animateQueue <- $animate <- $$interimElement <- $mdDialog <- alumDialogFactory <- HttpHandler <- $http <- $templateFactory <- $view <- $state

dialog.factory.js
------------------------
angular
.module('app.factory.Dialog')
.factory('DialogFactory', ['$mdDialog', function($mdDialog) {
    return {
        alert: function($mdDialog, title, msg) {
            $mdDialog.show(
                $mdDialog.alert()
                .parent(angular.element(document.querySelector('#popupContainer')))
                .clickOutsideToClose(false)
                .title(title)
                .textContent(msg)
                .ariaLabel(title)
                .ok('OK')
                .targetEvent()
            );
        }, 
}]);

1 个答案:

答案 0 :(得分:1)

你可以动态地注入任何依赖:

.factory('DialogFactory', ['$injector', function($injector) {
    return {
        alert: function(title, msg) {
            var $mdDialog = $injector.get('$mdDialog');
            ...
        }