admin管理员组文章数量:1325674
I am working on a project which is heavily form based. At times there will be about 1000 form elements in a page and there is option for users add more elements dynamically.
This causing performance problems. In most cases I don't need the two-way binding of ng-model
whenever user changes the input value, but I have to bind the value to scope only when user clicks the submit or next button.
Is there any simple way to do this, or should create my own alternate version of ng-model
? My aim is to reduce the $watch
es on my page.
I am working on a project which is heavily form based. At times there will be about 1000 form elements in a page and there is option for users add more elements dynamically.
This causing performance problems. In most cases I don't need the two-way binding of ng-model
whenever user changes the input value, but I have to bind the value to scope only when user clicks the submit or next button.
Is there any simple way to do this, or should create my own alternate version of ng-model
? My aim is to reduce the $watch
es on my page.
- Write a function to loop through all input elements and push their values into an array before the form is submitted? – Nate Barbettini Commented Feb 3, 2015 at 0:51
- Don't you need real time form validations? it's gonna be hard to only update the model on submit in that case – yangli-io Commented Feb 3, 2015 at 1:20
2 Answers
Reset to default 7With 2000 form items, and 2-way data-binding, the form is still quite responsive. If you really need better performance, you can specify that you only want the model to update on the "blur" event, by adding an ngModelOptions directive:
ng-model-options="{ updateOn: 'blur' }"
var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.items = [];
for (var i = 0; i < 2000; ++i) {
$scope.items.push({
name: 'item ' + i
});
}
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<div ng-app="app" ng-controller='ctrl'>
<ul>
<li ng-repeat="item in items">
<input type="text" ng-model-options="{ updateOn: 'blur' }" ng-model="item.name" />{{ item.name }}
</li>
</ul>
</div>
If you are using Angular ~1.3, you can improve in following part to enhance the performance.
Controlling when the model value updates
AngularJS 1.3 has a new feature called ngModelOptions to help users control how ngModel works on the input element. One useful feature is value debouncing
.
ng-model-options="{ debounce : { 'default' : 500 } }
In above code, the model will updated if the user has not typed in anymore characters for 500 milliseconds.
ng-model-options="{ debounce : { blur : 0 } }"
With above configuration, the model value and validations are applied immediately after when the user blurs out of the field
Use one-way binding
As official documents say:
One-time expressions will stop recalculating once they are stable, which happens after the first digest
We can apply the one-time expression by starting expressions with ::
, like changing <p>Hello {{name}}!</p>
to <p>Hello {{::name}}!</p>
Reduce blocking/time consuming expression
This is a generic rule for all Angular versions. Normally you'll not encounter performance issues until the amount of watchers is larger 2000. But if perf sitll sucks even the watchers are not that much, you may want to take a look at the expressions. As these expression will be evaluated each time digest loop
executes, any time consuming expression will slow the page rendering.
本文标签:
版权声明:本文标题:javascript - ng-model is overkill for me. Any alternative which will update scope only on button click not every time? - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742149993a2423013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论