Optimizing rendering using ChangeDetectionStrategy All Versions
This draft deletes the entire topic.
Examples
-
Consider the following component with one input
myInput
and an internal value calledsomeInternalValue
. 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 toChangeDetectionStrategy.Default
; implicit in the example. In this situation, any changes to any of the values in the template will trigger a re-render ofMyComponent
. In other words, if I changemyInput
orsomeInternalValue
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 toChangeDetectionStrategy.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:
toChangeDetectionStrategy.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.
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft