mark nebrat
mark nebrat

Reputation: 509

How to set ChangeDetectionStrategy.OnPush as default strategy

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?

Upvotes: 50

Views: 11745

Answers (1)

Pingless
Pingless

Reputation: 968

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"
      }
    }
  }
}

Upvotes: 71

Related Questions