2

I've been going through the guide on angular.io and came across the section on the NgClass directive. In the example provided the ngClass' source is a component function:

// HTML
<div [ngClass]="setClasses()">This div is saveable and special</div>

// Controller
setClasses() {
  let classes =  {
    saveable: this.canSave,      // true
    modified: !this.isUnchanged, // false
    special: this.isSpecial,     // true
  };
  return classes;
}

Does this not create a large overhead during the digest cycle considering the directive does not know when the result of the function has changed and would trigger a new evaluation of the function during each digest cycle? In contrast I'd expect the following code to provide the same functionality but to only re-evaluate specifically when one of the observed values have changed (canSave, isUnchanged, isSpecial).

<div [ngClass]="{ saveable : canSave, modified : !isUnchanged, special: isSpecial }">This div is saveable and special</div>

Could someone shed some light on what I should take into account to optimize performance? An example use case would be to have this ngClass on an ngRepeat that creates ~200 elements on the visible page.

As a side node and smaller question I was wondering if there is any good resource to learn about one-time binding (in angular2 vs angular1). The guide does not appear to cover this and I was hoping to have an async one time bind available in angular2.

1 Answer 1

2

Does this not create a large overhead during the digest cycle considering the directive does not know when the result of the function has changed and would trigger a new evaluation of the function during each digest cycle? In contrast I'd expect the following code to provide the same functionality but to only re-evaluate specifically when one of the observed values have changed (canSave, isUnchanged, isSpecial).

Your conclusion is correct.

The setClasses method returns a different instance for each call, which makes comparison in ngClass more expensive. If the same instance is returned as long as no dependency changed, then binding to a method this way is fine.

This was addressed recently very. https://github.com/angular/angular.io/issues/3072

As a side node and smaller question I was wondering if there is any good resource to learn about one-time binding (in angular2 vs angular1). The guide does not appear to cover this and I was hoping to have an async one time bind available in angular2.

Angular2 doesn't support one-time binding.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah. Thank you for the clarification! I ran into this last week but didn't realize it was already adjusted. Previous experience with angular taught me to always take performance into consideration and this would have been a major pitfall.
Exactly, this is why they changed it.
Late addition to the answer, but Angular does not have a digest cycle per se. It uses zones for isolated change detection.
You are right. It should be "change detection" but it's a quote and I assume "digest cycle was just used to make it easier for devs coming from Angular1 to understand what is meant.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.