angularjs - 如何使用 Angular-Strap 创建带有 Controller 的模态?

标签 angularjs modal-dialog angular-strap

我正在努力寻找正确的方法来使用 Angular-Strap 模态/除了 Controller 。

是的,调用代码可以注入(inject) $scope ,使其可用于模态。但这也有问题。

myModal = $modal({
scope: $scope,
template: 'template.html',
show: false,
backdrop: "static",
keyboard: false,
persist: true

});

这将污染调用 Controller 的潜在模式方法和属性。

我通常使用“controllerAs”,因此甚至没有 $scope首先注入(inject)模态!

您可以创建一个新的 $scope然后将方法插入其中,但同样需要注入(inject) $scope进入父 Controller 。坏坏坏。

如果我使用 ng-controller在模态模板中,我可以拥有我的 Controller 。但是他给了我另一个问题:现在我无法将数据注入(inject)模态 Controller ,并且我的调用代码无法知道模态何时关闭并且从模态返回数据也成为一件苦差事(涉及一个工厂只是为了保持父子 Controller 数据同步)。

我真的很努力如何使它成为最好的方法。

有任何想法吗?

干杯

更新

这就是我现在的做法:

在我的模板中,我创建了一个打开模式的指令。
例子:
<my-modal
        on-update="ctrl.OnDialogUpdate">
</my-modal>

所以基本上指令调用我的模态,当模态关闭或想要返回结果时,它调用指令参数中指定的方法。

这就是指令的外观:
(function() {

'use strict';

angular.module('app').directive('myModal',myModal);

function myModal(){

    return {

        restrict: 'E',

        // The modal callback specified in the directive tag
        scope: {
            onUpdate: '&?'
        },

        replace: true,

        // This is the template for the directive, not the modal
        templateUrl: 'button.html',

        controllerAs: 'ctrl',

        bindToController: true,

        compile: function (element, attrs) {

            return function (scope, element, attrs) {

            };
        },


        /*@ngInject*/
        controller: function($scope, $log, $aside){

            var self = this;

            var myDialog = $aside({

                // Dialog template
                template: 'my-modal.template.html',
                show: false,
                animation: 'am-fade-and-slide-right',
                placement: 'right',
                backdrop: true,
                html: true,
                container: '',
                scope: $scope
            });


            // Opens modal
            self.ShowDialog = function(){
                myDialog.$promise.then(function() {
                    myDialog.show();
                })
            };


            // Expose Update() method to the dialog template
            $scope.Update = function(){

                if(angular.isFunction(self.onUpdate) ) {

                    self.onUpdate()();
                }

            }

        }
    }

}

})();

最佳答案

只需使用“ Controller ”选项:

$scope.showModal = function() {
  $modal({
    title: 'My Title', 
    content: 'My Content', 
    show: true,
    controller: 'ModalCtrl'
  });
};

Here's a plnkr

关于angularjs - 如何使用 Angular-Strap 创建带有 Controller 的模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29098741/

相关文章:

javascript - Angular - 如何对不同类型的错误进行动态警报?

javascript - 如何连接 Angular Strap 模态显示事件

javascript - yam.platform.getLoginStatus 在没有 response.user 的情况下调用回调

javascript - 触发引导模式隐藏 (hide.bs.modal) 事件后,$location.path 需要一些时间才能工作

javascript - 无法关闭 ng-bootstrap 模式

javascript - 打开模态视图时禁用主体滚动

javascript - Angular 访问 Controller 内的 cookie

angularjs - 在不使用 $rootScope 的情况下使用 Angular ui 路由器防止 stateChange

angularjs - 如何在 AngularJS 的 Run block 中等待 Promise?

angularjs - 无法在 AngularStrap 上重现日期选择器示例