angularjs从指令调用模态对话框

时间:2013-04-16 09:37:22

标签: angularjs angularjs-directive angular-ui-bootstrap

我已经创建了一个打开模态对话框的指令。这是代码:

[编辑] JSFIDDLE在这:http://jsfiddle.net/graphicsxp/fcQZk/8/

问题:关闭的按钮不会关闭模式。

angular.module('person.directives').
directive("person", function($dialog) {
return {
    restrict: "E",
    templateUrl: "person/views/person.html",
    replace: true,
    scope: {
        myPerson: '='
    },
    link: function (scope, element, attrs) {

    },
    controller: function ($scope)
    {            
        $scope.opts = {
            backdrop: true,
            keyboard: true,
            backdropClick: true,
            templateUrl: 'person/views/searchPerson.html'
        };

        $scope.openDialog = function () {
            var d = $dialog.dialog($scope.opts);
            d.open().then(function (result) {
                if (result) {
                    alert('dialog closed with result: ' + result);
                }
            });
        }
    }
}

});

工作正常。现在,在对话框中,我使用另一个指令:

 <search-person></search-person>

和js:

angular.module('person.directives').directive("searchPerson", function ($dialog) {
return {
    restrict: "E",
    template: "<div>some template</div>",
    scope: {},
    link: function (scope, element, attrs) {

    },
    controller: function ($scope)
    {
        $scope.close = function (result)
        {
            $dialog.close(result);
        }
    }
}
});

但$ dialog.close(结果)没有做任何事情。我注意到$ scope.close为null。 我怀疑它与注射有关。我正在向searchPerson指令注入$ dialog,而我想我应该使用从person指令打开的对话框。我只是不知道......有什么想法吗?

[编辑2]

我已经替换了modal的模板和指令searchPerson的模板。现在我没有范围问题(请参阅更新的jsfiddle)。但关闭按钮仍然无法正常工作!错误是:

Object #<Object> has no method 'close'

似乎$ dialog未在searchPerson指令中正确注入....

3 个答案:

答案 0 :(得分:0)

我认为函数参数中的$对话框是'$ dialog'服务,而不是对话框对象的实例。这就是没有密切方法的原因。

答案 1 :(得分:0)

@Joe,实际上它是对话框对象,而不是$ dialog服务。在这种情况下,$ dialog服务的open()方法会向指定的控制器注入对话框的引用。

经过大量的摆弄,我通过将整个模态移动到searchPerson指令本身来解决问题。他们共享同一个控制器,这很好。

答案 2 :(得分:0)

我有类似的需求,所以我只是将对话框的控制器(我称之为“dialogController”)插入到$ scope.opts对象中。 像这样:

.directive('infoDialog', function($dialog) {
    return {
        restrict: 'A',
        link: function(scope, elm, attrs) {
            elm.bind('click', function() {
                scope.$apply(function() {
                    var info = { title:   attrs.title, content: attrs.content };
                    scope.openDialog(info);
                });
            })
        },
        controller: function($scope) {
            var dialogController = function($scope, dialog, info) {
                $scope.close = function(){ dialog.close(); };
                if(info){ $scope.info = info; }
            };

            $scope.opts = {
                backdrop:       true,
                keyboard:       false,
                backdropClick:  true,
                backdropFade:   false,
                resolve: { },
                controller:     dialogController,
                templateUrl: 'partials/dialogs/blank.html'
            };

            $scope.openDialog = function (info) {
                $scope.opts.resolve.info = function(){ return info; };
                var d = $dialog.dialog($scope.opts);
                d.open();
            }
        }
    };
});

@Sam,我发现你已经解决了这个问题,但这段代码可能对其他人有用。

相关问题