How to set the ChangeDetectionStrategy.OnPush as default strategy for every
component in my app instead of writing on every component's template
changeDetection: ChangeDetectionStrategy.OnPush
?
Asked
Active
Viewed 7,819 times
39

Dharman
- 30,962
- 25
- 85
- 135

mark nebrat
- 399
- 3
- 4
-
2Possible duplicate of [Angular2 Change the default change detection strategy](https://stackoverflow.com/questions/37821998/angular2-change-the-default-change-detection-strategy) – Mateusz Witkowski Jan 23 '18 at 16:42
1 Answers
53
It's not possible to set the global ChangeDetection.
However, it is possible to set it on the CLI, so that all components newly generated using ng generate component
(or ng g c
) will have it set to OnPush
.
Run this command to set it:
ng config schematics.@schematics/angular.component.changeDetection OnPush
Alternatively (this is what this command does), add this block at the base level of your angular.json
:
// angular.json
{
//...
"schematics": {
"@schematics/angular": {
"component": {
"changeDetection": "OnPush"
}
}
}
}

Pingless
- 788
- 1
- 7
- 9
-
But I create components by hand. And what if I later realize that this was not a good idea, we'll need to change all of these components again so that really defeats the purpose of setting a default change detection strategy. – Muhammad bin Yusrat Dec 20 '21 at 06:22