We are no longer accepting contributions to Documentation. Please see our post on meta.

Angular 2

Optimizing rendering using ChangeDetectionStrategy All Versions

2.0.0-rc.0
2.0.0-rc.1
2.0.0-rc.2
2.0.0-rc.3
2.0.0-rc.4
2.0.0-rc.5
2.0.0-rc.6
2.0.0-rc.7
2.0.0
2.0.1
2.0.2
2.1.0
2.2.0
2.3.0
4.0.0
4.1.0
4.2.0
4.3.0
4.3.1
4.3.2
4.3.3

This draft deletes the entire topic.

Examples

  • 3

    Consider the following component with one input myInput and an internal value called someInternalValue. Both of them are used in a component's template.

    import {Component, Input} from '@angular/core';
    
    @Component({
      template:`
      <div>
        <p>{{myInput}}</p>
        <p>{{someInternalValue}}</p>
      </div>
      `
    })
    class MyComponent {
      @Input() myInput: any;
    
      someInternalValue: any;
    
      // ...
    }
    

    By default, the changeDetection: property in the component decorator will be set to ChangeDetectionStrategy.Default; implicit in the example. In this situation, any changes to any of the values in the template will trigger a re-render of MyComponent. In other words, if I change myInput or someInternalValue angular 2 will exert energy and re-render the component.

    Suppose, however, that we only want to re-render when the inputs change. Consider the following component with changeDetection: set to ChangeDetectionStrategy.OnPush

    import {Component, ChangeDetectionStrategy, Input} from '@angular/core';
    
    @Component({
      changeDetection: ChangeDetectionStrategy.OnPush
      template:`
      <div>
        <p>{{myInput}}</p>
        <p>{{someInternalValue}}</p>
      </div>
      `
    })
    class MyComponent {
      @Input() myInput: any;
    
      someInternalValue: any;
    
      // ...
    }
    

    By setting changeDetection: to ChangeDetectionStrategy.OnPush, MyComponent will only re-render when its inputs change. In this case, myInput will need to receive a new value from its parent to trigger a re-render.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Optimizing rendering using ChangeDetectionStrategy? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.