瓶颈不是摘要(这会有所贡献,但可以有效地解决)ngModelOptions“去抖”选项 https://docs.angularjs.org/api/ng/directive/ngModelOptions#usage) but 添加的侦听器textarea指示 https://github.com/angular/angular.js/blob/v1.4.8/src/ng/directive/input.js#L1116 when ng-model
用在元素上。
这种性能问题建议使用自定义指令而不是内置指令(ngModel
在这种情况下)。对于双向绑定可能是
app.directive('bigText', function () {
return {
scope: {
bigText: '='
},
template: '<textarea>',
link: function (scope, element) {
// .val(value) on directive init
var initialized = false;
// prevents .val(oldValueFromOutside) on input
var internalChange = false;
var $textarea = element.find('textarea');
scope.$watch('bigText', function (oldVal, newVal) {
if (internalChange || (initialized && oldVal === newVal)) return;
initialized = true;
$textarea.val(newVal);
});
// generic JS debounce,
// for ex. https://github.com/niksy/throttle-debounce
var handler = debounce(1000, function () {
var text = $textarea.val();
if (scope.bigText !== text) {
internalChange = true;
scope.$apply(function () {
scope.bigText = text;
});
internalChange = false;
}
});
$textarea.on('change keyup', handler);
// $textarea.off on scope destroy
}
};
});
手动维护双向绑定的重要部分是使用internalChange
标志,它可以防止从外部范围获取旧值,如newVal
消化(先有鸡还是先有蛋的困境)。
从 Angular 指令对输入事件的处理中也可以学到一些东西为了表现 https://github.com/angular/angular.js/blob/v1.4.8/src/ng/directive/input.js#L1157 and 兼容性 https://github.com/angular/angular.js/blob/v1.4.8/src/ng/directive/input.js#L1164.
请注意,类似的性能问题可能是由以下原因引起的Chrome 错误 https://code.google.com/p/chromium/issues/detail?id=237433而不是通过 Angular 应用程序。