- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在下面的示例中,我不明白为什么在删除元素后 Angular 仍在触发解析器和格式化程序。我应该在指令中手动清理 ngModel Controller 吗?如果是这样,我应该怎么做?
要了解我在说什么,请查看 plunker,然后
笨蛋:http://plnkr.co/edit/R7v5nB8JaQ91WcDGU8BC?p=preview
angular.module('testMod', [])
.controller('testCtrl', function($scope){
$scope.test = "test";
$scope.removeElem = function(id) {
var elem = document.getElementById(id);
angular.element(elem).remove();
}
}).directive('testDir',[function() {
return {
require: 'ngModel',
scope:true,
link: function(scope, elem, attr, ctrl) {
console.log('in directive');
ctrl.$parsers.unshift(function (newValue) {
console.log('directive parser');
return newValue;
});
ctrl.$formatters.unshift(function (newValue) {
console.log('directive formatter');
return newValue;
});
}
}
}]);
<body ng-controller='testCtrl'>
<input id='test' test-dir ng-model='test'/>
<button ng-click="removeElem('test')">remove</button>
<button ng-click="test = test + 'a'">change model</button>
<div>{{test}}</div>
</body>
最佳答案
您的指令创建了自己的子作用域 - 这是一件好事。它拥有其上的所有 $watches,并且应该在其作用域被销毁后自行清理。
最好的做法是:
在设置 $watches 并计划稍后销毁该范围时创建一个子范围或隔离范围。由于该指令创建了范围,它也应该是在必要时销毁它并释放其所有 $watches 的指令。
检测元素何时被移除并在必要时销毁作用域。有时,如果您的 $watches 很重,并且您不希望它们在指令绑定(bind)到的元素从 DOM 中删除时徘徊,那么这样做有时是有意义的。如果删除是临时的——即切换元素的可见性,这可能没有意义
指令不应该破坏属于另一个的范围。即,如果您从父 Controller 继承范围,则让父 Controller 自行清理 - 这不是子指令的工作。
如果指令设置了任何 $watches,它应该监听范围 $destroy 事件,以便它可以注销它们。
如果指令已使用 on() 注册了任何 javascript 事件监听器,它应该在销毁作用域时使用 off() 注销它们。
通过处理 jQuery 元素 本身的 $destroy 事件,可以在从 DOM 中删除元素时执行清理:
// creates a child scope owned by the directive
scope: true,
link: function(scope, element, attr) {
// set up a click handler
element.on('click', function() {
...
});
// set up a $watch on the child scope. It returns a
// function that you can call to unregister the $watch
var unregister = scope.$watch('test', function() {
...
});
// when the element is removed from the DOM,
// destroy the scope and all its $watches.
element.on('$destroy', function() {
// remove the $watch
unregister();
// remove the click handler
element.off('click');
// call $destroy on the child scope
// so that it can propagate to any children
scope.$destroy();
});
}
您不应该必须在 ng-model 之后进行清理。当 $destroy 事件传播到子范围时,它将自行清理。如果所有指令都自行清理,那么在内存泄漏和孤立的 $watches 方面就不用担心了。
这是更新的 Plunker当元素被移除时,它会清理它的 $watches。
关于javascript - angularjs: ngModel controller $parser & $formatter 在元素被移除后仍然触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701705/
每次我尝试构建(执行完整的 Clean,然后构建)时,我都会在 Eclipse 的问题部分下弹出此错误消息。项目本身旁边还显示一个错误。 我已经尝试了同一问题的其他解决方案中包含的所有内容: 删除项目
我收到以下错误(注意:我使用的是 Netbeans): java.sql.SQLException: No suitable driver found for jdbc:derby://localho
例如 //somewhere struct IFace; struct Base { Base(IFace* iface): f(iface) { //wi
我试图通过 stringstream 将 double 变成字符串,但它不起作用。 std::string MatlabPlotter::getTimeVector( unsigned int xve
我正在尝试使用 AudioKit 框架中的音序器播放音频文件。 AudioKit.output = sampler AudioKit.start() sampler.enableMID
昨天我问了一个关于插入 Heroku 的问题。它不工作,然后突然开始工作。我什么都没改变。现在在一个新的应用程序上,我遇到了完全相同的问题。我决定包含我的整个 Gemfile,希望我可以继续没有这种令
我知道,这个topic已经是discussed许多times,所以直截了当。 这是ItemsSource的TabControl: Tabs = new ObservableCollection {
我有一个更新对象的函数,问题是当我从更新表单字段返回到详细 View 时,它初始化旧对象而不是更新后的对象。 我想在 CarService 而不是 app.js 中填充汽车列表 这是我的汽车服务:
在 resolution comments错误报告 12266 (“套接字连接错误导致资源泄漏”),Robert Ehteshamzadeh 写道 TClientSocket is deprecate
我最初发布了一个问题 here 我发现 JTextField 仅在 JScrollPane 存在时才调整大小。换句话说,我可以根据需要最小化和最大化它,直到出现滚动条(因为文本太多,无法放入窗口)。之
我读过关于 postion:absolute 的问题并尝试了几乎所有可能的解决方案。包括相对定位 div,将它们包装在相对定位的父级中等等,但它没有帮助。 我正在绘制一个表格,然后我将 div 放入其
我在这里发起了一个话题document.getElementById not working但看起来即使提出的建议都是有效的,我仍然有问题。 我有几个复选框。当我在这里查看页面源代码时,有。 docu
我正在做一些阅读,试图更好地理解按位运算符,然后偶然发现了 a helpful old blog post from 2012 ,其中指出 - 在随机正整数 x 的奇数测试中 - 在作者的计算机上评估
我正在尝试在 Eclipse Neon 中使用 aspectj 创建一个示例 maven 项目。然而,方面并没有编织/工作(参见下面的输出)。我尝试寻找很多原因和解决方案,但没有一个有效(请参阅下面的
无论我如何配置我的 appsettings.json 和 appsettings.Development.json,除非我手动添加 ConfigureLogging,否则我无法在信息消息下方记录任何内
我正在尝试使用 JQuery .get() 方法和 JavaScript for 循环来处理来自外部文件的一些数据。我已经在 stackoverflow 上阅读了有关闭包和回调返回值的内容几个小时,但
我正在使用 PHP 5.6 并且要打印一些东西,我必须编辑 php.ini 并包含 php_printer.dll 文件。但是 PHP 5.6 没有.dll 文件。 我要解决的问题: 我想将凭证打印机
我目前正在调试一个包含内存泄漏的大(非常大!)C# 应用程序。它主要使用 Winforms 作为 GUI,尽管一些控件是在 WPF 中制作的,并由 ElementHost 托管。直到现在,我发现许多内
[已解决] 看来 PHP MYADMIN 变量成功了。我将 wait_timeout 设置为 30 ,并将 Lock_wait_timeout 设置为 50 花了将近 6 个小时才恢复稳定,包括几次重
我读过几个关于该主题的讨论,有人说 qmake < 3.0 不正确支持该指令。我刚刚为 g++-64 重新安装了 Qt 5.9.1,但问题仍然存在。此外,我尝试过各种 mkspecs/xxx/xxx.
我是一名优秀的程序员,十分优秀!