html – destroy指令/子范围破坏

前端之家收集整理的这篇文章主要介绍了html – destroy指令/子范围破坏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个指令,编译另一个指令,并将其附加到身体,具有相同的范围通过.这与“父”指令的位置不一样.

当父指令被破坏时,有没有办法让孩子的指令和范围也被破坏?我问,因为在检查了DOM后,孩子的指令还在.

目前我挂钩父母$destroy事件,但如果能自动处理好奇心,

jsFiddle:http://jsfiddle.net/FPx4G/1/

孩子在家中转过身去,但我想要被摧毁.这样做最好的方法是什么?

HTML:

<div ng-app="app">
    <div ng-controller="ParentCtrl">
        <button data-ng-click="toggleParent()">Toggle Parent</button>
        <div data-ng-switch data-on="displayDirective">
            <div data-ng-switch-when="true">
                <div class="parentDirective">Parent Directive</div>
            </div>
        </div>
    </div>
</div>

JavaScript的:

angular.module('app',[])
    .directive("parentDirective",function($compile){
        return {
            restrict: 'C',link: function(scope,element){
                var secondDirective = angular.element(document.createElement("div"));
                secondDirective.addClass("childDirective");

                document.body.appendChild(secondDirective[0]);

                $compile(secondDirective)(scope);
            }
        }
    })
    .directive("childDirective",function(){
        return {
            restrict: 'C',template: '<div>Child Directive</div>',element){
                scope.$on("destroy",function(){
                   alert(1); 
                });
            }
        }
    });


function ParentCtrl($scope){
   $scope.displayDirective = true;
    $scope.toggleParent = function(){
        $scope.displayDirective = !$scope.displayDirective;
    }
}

通常,我只需要在原始指令的模板中设置子元素,使其正确定位.这个问题真的归结于z-index的处理.父元素位于可以滚动的容器中,因此如果小于该容器的大小,则该子项(在一种情况下为自定义下拉列表)将被隐藏/切断.为了打击这一点,我在文档主体中创建实际的子节点,并将其相对于父节点定位.它也会听滚动事件重新定位自己.我有一切工作都很好.当我需要删除父母时会发生什么…孩子还在那里.

解决方法

directive("childDirective",function(){
    return {
        restrict: 'C',template: '<div >Child Directive</div>',element){                  
            scope.$on("$destroy",function() {
                element.remove();
            }); 
        }
    }
});

更新小提琴:http://jsfiddle.net/C8hs6/

原文链接:https://www.f2er.com/html/229532.html

猜你在找的HTML相关文章