3

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 $watches on my page.

2
  • Write a function to loop through all input elements and push their values into an array before the form is submitted? 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 2

7

With 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.com/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>

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.