0

我创建了一个指令来打开一个模式对话框。这是代码:

[编辑] 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(result) 没有做任何事情。我注意到 $scope.close 为空。我怀疑这与注射有关。我将 $dialog 注入到 searchPerson 指令中,而我想我应该使用从 person 指令打开的对话框。我只是不知道...有什么想法吗?

[编辑 2]

我已经用指令 searchPerson 替换了模态的模板。现在我没有范围问题(请参阅更新的 jsfiddle)。但是关闭按钮仍然不起作用!错误是:

Object #<Object> has no method 'close'

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

4

3 回答 3

0

我认为函数参数中的 $dialog 是“$dialog”服务,而不是对话对象的实例。这就是为什么没有关闭方法的原因。

于 2013-04-17T23:14:07.727 回答
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,我看到您已经解决了这个问题,但是这段代码可能对其他人有用。

于 2013-12-07T02:24:14.103 回答
0

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

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

于 2013-04-18T06:21:18.380 回答