相关疑难解决方法(0)

销毁范围破坏的指令/子范围

我有一个指令,编译另一个指令并将其附加到具有相同范围的主体.这与"父"指令的位置不同.

当父指令被销毁时,有一些方法可以让子指令和范围也被销毁吗?我问,因为在检查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>
Run Code Online (Sandbox Code Playgroud)

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>',
            link: function(scope, element){
                scope.$on("destroy", function(){
                   alert(1); 
                });
            }
        }
    });


function ParentCtrl($scope){
   $scope.displayDirective = true;
    $scope.toggleParent = function(){
        $scope.displayDirective = …
Run Code Online (Sandbox Code Playgroud)

html angularjs

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

angularjs ×1

html ×1