私はAngularのダイジェストサイクルと、それが長いリストと大きなモデル値でパフォーマンスにどのように影響するかをよく知っています。私の場合に特に問題の回避策があるかどうか、私はただ興味があります。ユーザーがエラーログなどの大きなテキストを入力する必要がある場合とない場合があるアプリを構築していますが、大きなモデルでのtextarea
双方向データバインディングに関連する角度固有の問題が原因で、アプリがハングします。
現在、ファイルに大きなコンテンツを添付するようにユーザーに求めています。しかし、私はこの問題を回避する方法があるかどうか疑問に思いました。私が考えることができる1つの潜在的な回避策は、テキストフィールドでモデルを使用せず、angularの軽量jqueryapiを使用してテキストにアクセスすることですangular.element('#mytext').val()
。ただし、このアプローチには、JSコードをDOMに緊密にバインドするという欠点があります。これは適切な方法ではありません。もっと「角度のある」スタイルのソリューションに興味があります。
これがパフォーマンスの問題を示すプランカーです。ハングを確認するには、テキスト領域に入力を開始すると、深刻な遅延が発生します
The bottleneck is not a digest (which would contribute but could be effectively fought with ngModelOptions
'debounce' option) but a listener added by textarea
directive when ng-model
is used on the element.
This kind of performance issues suggests the usage of custom directive instead of built-in ones (ngModel
in this case). For two-way binding it may be
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
}
};
});
The important part in maintaining two-way bindings manually is using internalChange
flag, it prevents getting old value from outer scope as newVal
on digest (chicken/egg dilemma).
Some things may also be learned from the treatment that Angular directives give to input events for performance and compatibility.
同様のパフォーマンスの問題は、AngularアプリケーションではなくChromeのバグが原因である可能性があることに注意してください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加