角度转换包含ng-template的指令(通用确认模式)

时间:2015-03-27 16:07:47

标签: javascript angularjs modal-dialog angular-bootstrap transclusion

你好我在创建基于angular-bootstrap Modal指令的通用确认指令时遇到了困难。

我找不到在用于模态构造的ng-template中转换我的内容的方法,因为ng-transclude指令未被评估,因为它是执行后加载的ng-template的一部分$modal.open()

index.html(指令插入):

<confirm-popup
    is-open="openConfirmation"
    on-confirm="onPopupConfirmed()"
    on-cancel="onPopupCanceled()"
>
Are you sure ? (modal #{{index}})

confirmPopup.html(指令模板):

<script type="text/ng-template" id="confirmModalTemplate.html">
    <div>
        <div class="modal-header">
            <h3>Confirm ?</h3>
        </div>
        <div class="modal-body">
            {{directiveTranscludedContent}} // ng-transclude do not work here
        </div>
        <div class="modal-footer">
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
            <button class="btn btn-primary" ng-click="ok()">Validate</button> 
        </div>
    </div>
</script>

confirmPopup.js(指令JS):

.directive('confirmPopup', [
    function() {
        return {
            templateUrl: 'confirmPopup.html',
            restrict: 'EA',
            replace: true,
            transclude: true,
            scope: {
                isOpen: '=',
                confirm: "&onConfirm",
                cancel: "&onCancel"
            },
            controller: ['$scope', '$element', '$modal', '$transclude', '$compile', function($scope, $element, $modal, $transclude, $compile) {

              // watching isOpen attribute to dispay modal when needed
                $scope.$watch(
                    function() {
                        return $scope.isOpen;
                    },
                    function(newValue) {
                        if (newValue === true) {
                            openModal();
                        } else {
                            // if a modal is already dispayed : the modal must be canceled/confirmed by the user
                            // else (if no modal is dispayed), then do nothing
                        }
                    }
                );

                // open modal function
                // create / register ok/cancel callbacks
                // and open modal
                // all on one shot
                function openModal() {

                    $modal.open({
                        templateUrl: 'confirmModalTemplate.html',
                        controller: ['$scope', '$modalInstance', 'content', function($scope, $modalInstance, content) {

                            $scope.directiveTranscludedContent = content;

                            $scope.ok = function() {
                                $modalInstance.close();
                            };

                            $scope.cancel = function() {
                                $modalInstance.dismiss();
                            };
                        }],
                        resolve: {
                            content: function() {
                                return $transclude().html();
                                      //return $compile($transclude().contents())($scope);
                            },
                        }
                    })
                    .result.then(
                        // modal has been validated
                        function() {
                            $scope.confirm();
                        },
                        // modal has been dismissed
                        function() {
                            if ($scope.cancel) {
                                $scope.cancel();
                            }
                        }
                    );
                };
            }]
        };
    }
]);

如果不够清楚,请点击此PLUNKER我只是在点击“Are you sure ? (modal #2)”按钮时才会看到“open confirm modal #2”。

1 个答案:

答案 0 :(得分:3)

ui-bootstrap模式仅支持templatetemplateUrl作为指定内容的方式。但是,检索内容时,$modal(或更确切地说,内部$modalStack)服务会根据提供的范围对其进行编译和链接。

所以,至少,像这样,没有办法提供翻译。

一种方法是嵌入一个占位符指令,该指令将附加被转换的DOM - 但是由于它来自不同于模态的位置,所以需要以某种方式将该转换的DOM移交给该占位符指令。您已将content作为注入的resolve参数。我将稍加修改使用它 - 我将传递实际的DOM,而不是解析的HTML。

所以,在高层:

.directive("confirmPopupTransclude", function($parse){
  return {
    link: function(scope, element, attrs){
      // could have been done with "=" and isolate scope, 
      // but avoids an unnecessary $watch
      var templateAttr = attrs.confirmPopupTransclude;
      var actualTemplateDOM = $parse(templateAttr)(scope);

      element.append(actualTemplateDOM);
    }
  };
})

并且,在openModal函数中(省略不相关的属性):

function openModal{
   $modal.open({
     controller: function($scope, content){
        $scope.template = content;
        // etc...
     },
     resolve: {
       content: function(){
         var transcludedContent;
         $transclude(function(clone){
           transcludedContent = clone; 
         });
         return transcludedContent; // actual linked DOM
       },
     // etc...
}

最后,在模态的实际模板中:

<div class="modal-body">
    <div confirm-popup-transclude="template"></div>
</div>

Your forked plunker