如果元素从javascript中移除,如何在angularjs中手动销毁作用域
How to destroy scope manually in angularjs if element is removed from javascript?
我插入角模板元素。考虑下面的例子,
<p>
<c ng-controller="ctrl">
...
</c>
</p>
如果我从javascript中删除c。会有什么副作用?(范围泄漏)如何避免这种情况?
我用过这个,
function render() {
var lastScope = angular.element('c').scope();
if(lastScope) {
lastScope.$destroy();
}
$('c').remove();
getTemplate(context + '/c.html', function(template) {
if (template) {
angular.element(document).injector().invoke(['$compile', '$rootScope', function($compile, $rootScope) {
$('p').append($compile(template)($rootScope));
$rootScope.$apply();
}]);
}
});
}
当我点击标签渲染函数每次被调用。还有其他建议吗?
创建新的作用域并销毁它,如下:
var newScope = $rootScope.$new(true);
$compile(template)(newScope);
//later
newScope.$destroy();
可以使用
$scope.$on("$destroy",destroyScope);
$scope.destroyScope =function(){
// here you can delete your this function will be called whenever you move out from you controller here you can destroy you scope
$('c').remove();
delete $scope.var1;
}
将作用域变量定义为像
这样的对象总是好的做法。$scope.currentControllerscope ={}
$scope.currentControllerscope.myVar1 ="string"
$scope.currentControllerscope.myVar2 ="string2"
这样你就可以像
那样销毁整个对象delete $scope.currentControllerscope
这里也有类似的问题提供一个scope's $destroy event?
相关文章:
- AngularJS$作用域在选项卡集中出现问题
- 为什么AngularJS$作用域不能正常工作
- 在另一个作用域变量中使用AngularJS作用域变量
- AngularJS$作用域在函数之外不可访问
- AngularJS$作用域包括我的绑定变量,但以后仍然无法访问,为什么
- 将元素绑定到不同帧中的现有 AngularJS 作用域
- ngModel 的 angularjs 作用域未按预期工作
- 使用循环简化 AngularJS 作用域
- AngularJS = 作用域中的 = 作用域导致解析语法错误
- 将 javascript 变量传递到 angularJS 作用域
- AngularJs 作用域 - 如何正确从 DOM 获取作用域
- 如何从事件处理程序调用 AngularJS 作用域定义的函数
- 在.then()单元测试中AngularJS$作用域变量的变化
- AngularJS作用域属性未定义
- Angularjs作用域的父id
- 单击链接时,将AngularJS$作用域变量传递给新控制器
- AngularJS作用域属性未定义
- 在jQuery中访问AngularJS作用域变量
- 将angularJs作用域的内容传递给javascript变量
- AngularJS作用域变量没有'当它在另一个函数内部发生更改时,它不会更新