20

I've decided to set ViewEncapsulation to None for all my components in my project.

So, how can I set ViewEncapsulation.None globally to whole project? instead of setting it in each of my components decorator.

Note: Just for extra info, My deps are on RC.6

Edit: The 2nd solution provided by Günter Zöchbauer also works on 2.1.2

choz
  • 17,242
  • 4
  • 53
  • 73

1 Answers1

34

You can set default view encapsulation by passing a custom CompilerConfig. This feature was added in RC.2 (last item in the features list)

1. You can set it on NgModule level

this way doesn't work anymore

    @NgModule({
        // imports, declaration, bootstrap, etc..
        providers: [{
            provide: CompilerConfig, 
            useValue: new CompilerConfig({
                defaultEncapsulation: ViewEncapsulation.None
            })
        }]
    })
    export class AppModule {}

Plunker example

2. Set it on bootstrap level (Thanks to yurzui)

platformBrowserDynamic().bootstrapModule(AppModule, [
    {
        defaultEncapsulation: ViewEncapsulation.None
    }
]);

Please note that you ofc need to import ViewEncapsulation from @angular/core

3. In more recent Angular versions this can be set in compiler options

Caution: I haven't tried this myself yet. This is just how I interpreted the docs:

In tsconfig.json

{ 
  ...,
  angularCompilerOptions: {
   defaultEncapsulation: 3;
  }
}
 
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for your information. I've tried your answer and still unable to get it working. Please check my updated question. Am I missing something? – choz Sep 12 '16 at 15:39
  • 3
    `platformBrowserDynamic().bootstrapModule(AppModule, { defaultEncapsulation: ViewEncapsulation.None });` – yurzui Sep 12 '16 at 15:45
  • @yurzui did you try it? I saw that `boostrapModule()` takes `CompilerOptions` but that seems to be something different than `CompilerConfig`. – Günter Zöchbauer Sep 12 '16 at 15:48
  • @GünterZöchbauer I confirm his approach also works here.. Actually, both of yours work great.. Thanks for the answer.. – choz Sep 12 '16 at 15:49
  • I forgot the `.None` in my code. Also just tried it https://plnkr.co/edit/DT3jJeXxDyhOm5r07y5b?p=preview and I can confirm it's working. – Günter Zöchbauer Sep 12 '16 at 15:52
  • If I set it at bootstrap level will it be applied to third party modules I use? – Dimanoid May 23 '17 at 11:37
  • 1
    I haven't tried. I'd expect that, if they set it explicitly, this setting will be used, but where they left the default (not specified it - currently probably almost always the case), the new default will be applied. – Günter Zöchbauer May 23 '17 at 11:40
  • first solution does not work now, please confirm and update the answer – Stepan Suvorov Jul 27 '17 at 07:41
  • 3
    This will not work wit AOT compilation, only with JIT. Now I have to use `--aot=false` for my production build. – artuska Oct 30 '17 at 08:23
  • I don't think this is limited to AoT. – Günter Zöchbauer Oct 30 '17 at 08:27
  • does this override settings that are on a component? – BlackICE May 05 '19 at 21:56
  • 1
    The answer is outdated. This setting was moved to NgModule as far as I remember and the component setting overrides the module setting. There is no way to override the component setting because the component code (HTML, CSS) probably breaks if you override it. – Günter Zöchbauer May 06 '19 at 03:44
  • 5
    None of the solutions seem to work any longer any idea how to globally set this in Angular 10? – Andy Braham Jul 09 '20 at 08:28
  • I updated my answer. Not sure it's any good. I just looked at the docs. – Günter Zöchbauer Jul 09 '20 at 09:44
  • It seems that it still doesn't work for JIT compiled components. There's a merge request but the edit hasn't been released yet: https://github.com/angular/angular/pull/35534 – valepu Jun 24 '21 at 13:18
  • 1
    According to [THIS](https://github.com/angular/angular/issues/12251#issuecomment-872613835) the 3rd option doesn't seem to be supported now... – Lyubomir Jun 26 '22 at 23:34