【问题标题】:How do I add a reusable modal dialog in Angular?如何在 Angular 中添加可重用的模态对话框?
【发布时间】:2014-10-10 02:19:38
【问题描述】:

我是 Angular 新手,正在尝试在我的项目中实现 this solution

它看起来非常简单,但是,我正在尝试将它变成一个可重复使用的元素,以便我可以从任何地方调用它并传入要显示的内容(否则,有什么意义?)。

所以,我的具体问题是:假设我已经有一个绑定到某个 DOM 元素的 controller,并且它有一个功能可以获取一些 factory 驱动的 $http 调用,并在响应时我希望通知用户通过这个对话框,我如何将 *this 指令和 *this 控制器与我现有的指令结合起来,以及我如何以一种允许我从完全不同的controller 再次使用它的方式进行操作?

或者这可能是这个用途的一个坏例子,我应该看一个不同的例子吗?

【问题讨论】:

    标签: angularjs modal-dialog reusability


    【解决方案1】:

    尝试将“ngDialog”库用于弹出窗口和模式。非常好的图书馆。稍后您可以创建一个内部调用 ngDialog 函数的服务。稍后可以将此服务注入您的控制器中以供使用。我已经在一个项目中实现了这一点。

    服务中的函数可以接受用于初始化 ngDialog 模式的参数。

    希望对你有帮助:)

    【讨论】:

    • 哇...这绝对是要走的路! :-) 不知道有什么限制,最终我想得到这个问题的答案(关于创建普遍可访问的元素),但非常感谢这个提示,非常有用的库。
    • 我不会将“ngDialog”库用于复杂的窗口。它不允许拖动和调整大小。在我的项目中,它破坏了动画。有时它会停止响应按钮上的任何点击,直到浏览器页面重新加载。
    【解决方案2】:

    为了使它更好,我建议您修改代码,使其如下所示

    模板:

    <div class='ng-modal' ng-show='modalContent != null && modalContent != ""'>
      <div class='ng-modal-overlay' ng-click='hideModal()'></div>
      <div class='ng-modal-dialog' ng-style='dialogStyle'>
        <div class='ng-modal-close' ng-click='hideModal()'>X</div>
        <div class='ng-modal-dialog-content' ng-transclude></div>
        <p>{{ modalContent }}</p>
      </div>
    </div>
    

    指令:

    app.directive('modalDialog', function() {
      return {
        restrict: 'E',
        scope: {
          modalContent: '='
        },
        replace: true, // Replace with the template below
        transclude: true, // we want to insert custom content inside the directive
        link: function(scope, element, attrs) {
          scope.dialogStyle = {};
          if (attrs.width)
            scope.dialogStyle.width = attrs.width;
          if (attrs.height)
            scope.dialogStyle.height = attrs.height;
          scope.hideModal = function() {
            scope.modalContent= null;
          };
        },
        template: '...' // See below
      };
    });
    

    然后在模板

    中使用如下代码
    <modal-dialog modal-content='modalMsg' width='750px' height='90%'></modal-dialog>
    

    完成这些更改后,您可以编写一个函数来在变量“modalMsg”中设置消息,而 Angular 将负责休息

    注意:代码与链接中的代码几乎相同,我唯一更改的是显示模式框的检查

    【讨论】:

    • 谢谢。我的困惑围绕着如何使任何控制器都可以普遍访问此代码。如何将此指令绑定到“someArbitraryController”以及在 DOM 中的哪个位置放置&lt;modal-dialog&gt; 和关联的&lt;div&gt;s?我将控制器模板作为单独的 HTML 文件路由到。我的愿望是能够从任何地方调用这个指令......这有意义吗?
    • &lt;modal-dialog&gt; 需要成为每个需要模式窗口的模板的一部分,然后您可以从其控制器设置文本(在用于modalContent 的变量中)。你不需要用这个绑定任何额外的控制器。我不是 100% 确定,但我猜你正在寻找像 Max 的 cmets 中的 ng-dialog 之类的东西。如果这就是您所需要的,那么您可能想要创建自己的提供商docs.angularjs.org/guide/providers(使用您的链接作为参考)。 ng-dialog 也是如此
    • 是的,我相信我自己的提供商是使其通用的唯一方法。我现在开始思考这个问题......(慢慢地)
    【解决方案3】:

    与其他选项相比,下面给出了极简主义的方法,使用角度 factory。请参阅下面的示例 sn-p。

    注意:使用 Angular JS 和 UI Bootstrap - AngularUI。

    1. 可重复使用的模式视图 - ConfirmationBox.html

    <div class="modal-header">
      <h3 class="modal-title">{{title}}</h3>
    </div>
    <div class="modal-body">
      {{message}}
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-primary btn-warn" data-ng-click="ok(); $event.stopPropagation()">OK</button>
    
      <button type="button" class="btn btn-default" data-ng-click="cancel(); $event.stopPropagation()">Cancel</button>
    </div>
    1. 可重用模块和共享工厂,用于处理可重用模态对话框

    angular.module('sharedmodule',['ui.bootstrap', 'ui.bootstrap.tpls'])
    .factory("sharedService",["$q", "$modal", function ($q, $modal)
    {
        var _showConfirmDialog = function (title, message)
        {
            var defer = $q.defer();
    
            var modalInstance = $modal.open({
                animation: true,
                size: "sm",
                templateUrl: 'ConfirmationBox.html',
                controller: function ($scope, $modalInstance)
                {
                    $scope.title = title;
                    $scope.message = message;
    
                    $scope.ok = function ()
                    {
                        modalInstance.close();
                        defer.resolve();
                    };
    
                    $scope.cancel = function ()
                    {
                        $modalInstance.dismiss();
                        defer.reject();
                    };
                }
            });
    
            return defer.promise;
        }
    
        return {
    
            showConfirmDialog: _showConfirmDialog
        };
    
    }]);
    1. 部分视图,使用共享模式对话框

    &lt;a data-ng-click="showConfirm()"&gt;Go Back to previous page&lt;/a&gt;
    1. 视图控制器,打开共享的可重用模式对话框并处理通知(确定和取消)

    var myModule = angular.module("mymodule", ['sharedmodule', 'ui.bootstrap', 'ui.bootstrap.tpls']);
    
    myModule.controller('myController', ["$scope", "sharedService", "$window",
    function ($scope, sharedService, $window)
    {
        $scope.showConfirm = function ()
        {
            sharedService.showConfirmDialog(
                'Confirm!',
                'Any unsaved edit will be discarded. Are you sure to navigate back?')
                .then(function ()
                {
                    $window.location = '#/';
                },
                function ()
                {
                });
        };
    }]);

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2019-03-23
      相关资源
      最近更新 更多