Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs从DOM中删除自定义指令和子指令_Angularjs_Dom - Fatal编程技术网

Angularjs从DOM中删除自定义指令和子指令

Angularjs从DOM中删除自定义指令和子指令,angularjs,dom,Angularjs,Dom,我有一个单页angularjs应用程序。我使用$routeProvider加载下面代码中显示的自定义指令 现在,加载的每个自定义指令都由附加的子自定义指令组成。所有自定义指令都有独立的作用域 我需要的是当视图更改时,要销毁的范围以及从当前视图下的DOM中删除指令。我已经得到了如下代码。这只能通过Jquery lite和/或angularjs实现吗?如果是这样,如何从DOM中删除特定视图的父和子指令?提前谢谢 自定义指令表 angular.module("form", []) .dire

我有一个单页angularjs应用程序。我使用$routeProvider加载下面代码中显示的自定义指令

现在,加载的每个自定义指令都由附加的子自定义指令组成。所有自定义指令都有独立的作用域

我需要的是当视图更改时,要销毁的范围以及从当前视图下的DOM中删除指令。我已经得到了如下代码。这只能通过Jquery lite和/或angularjs实现吗?如果是这样,如何从DOM中删除特定视图的父和子指令?提前谢谢

自定义指令表

 angular.module("form", [])
    .directive("form",['$http','$rootScope','$location', function($http,$rootScope,$location){

return{
            link: function(scope,element,attrs){ 
               //functions go heere

                //destroy scope and remove from DOM on route change
                $rootScope.$on( "$routeChangeSuccess", function(event, next, current) {
                  if($location.path()!=='/form'){
                        scope.$destroy();
                        console.log('This should not be displayed on route change');
                    }   
                });



                //function and scopes go here
            },//return
            restrict:"A", 
            replace:true,
            templateUrl:"partials/form/form.html",//template
            transclude:true, //incorporate additional data within
            scope:{}
        }//return
}])

ng视图/路由提供商

app.config(['$routeProvider',function($routeProvider) {
        //configure the routes
        $routeProvider

        .when('/',{
        // route for the home page
            templateUrl:'partials/login/login.html',
            controller:'loginCtrl'      
        })

        .when('/home',{
        // route for the home page

            templateUrl:'partials/home/home.html',
            template:'<div home></div>'         
        })

        .when('/company',{
        // route for the sites& companies
            template:'<div company></div>'      
        })

        .when('/form',{
        // route for form
            template:'<div form></div>' 
        })

        .otherwise({
            //when all else fails
            templateUrl:'partials/login/login.html',
            controller:'loginCtrl'
        });

}]);
app.config(['$routeProvider',函数($routeProvider){
//配置路由
$routeProvider
。当(“/”{
//主页的路径
templateUrl:'partials/login/login.html',
控制器:'loginCtrl'
})
.when(“/home”{
//主页的路径
templateUrl:'partials/home/home.html',
模板:“”
})
。当(“/公司”{
//站点和公司的路线
模板:“”
})
.when(“/form”{
//表格的路径
模板:“”
})
.否则({
//当一切都失败的时候
templateUrl:'partials/login/login.html',
控制器:'loginCtrl'
});
}]);

请参阅此参考资料:

根据来源参考:

步骤:
1.删除指令的范围
2.删除指令的DOM

childScope.$destroy();  //IF THE DIRECTIVE WAS CREATED DYNAMICALLY OR ELSE WE MAY USE angular.element(DIRECTIVES-DOM).scope().$destroy()
$('.my-directive-placeholder').empty(); //DELETE DIRECTIVE'S DOM