If you are working especially with very big projects, the OnPush
strategy is recommended to decrease the change detection process that it is a very expensive operation.
There are many ways of starting the detection when needed, maybe the most used is to trigger manually changeDetection()
from the ChangeDetectorRef
.
If you have an inherited project and you want to use the OnPush
strategy, the recommendation is to start applying it from the leaf components, check that everything is still working, and then follow the ancestors and go up one level at time to the root. In the end the overall performance is going to benefit.
Here there is a very good article about change detection in Angular.
If you want your new generated components to have automatically the OnPush
strategy added, you just need to add the option in your angular.json
at the schematics
node, for example:
...
"schematics": {
"@schematics/angular:component": {
"changeDetection": "OnPush",
"prefix": "app",
"styleext": "scss"
},
"@schematics/angular:directive": {
"prefix": "app"
}
}
...