How should I use the new static option for @ViewChild in Angular 8?How to use canvas in Angular2?Angular Material Table Sorting - Data Doesn't sort when inside a *ngIf conditionHow we set focus on input element in angular7Angular input interpolationHow do I use ViewChild for dynamic created html elements in Angular 2+?Cannot set property of undefined (when it is defined) in AngularHow can I check which version of Angular I'm using?How can I get new selection in “select” in Angular 2?In Angular, how do you determine the active route?Angular 2 @ViewChild annotation returns undefinedWhat are the “spec.ts” files generated by Angular CLi for?How to bundle an Angular app for productionAngular/RxJs When should I unsubscribe from `Subscription`How to detect when an @Input() value changes in Angular?How to generate components in a specific folder with angular-cli?instantiate @ViewChild on AfterViewInit angular 2

Does recycling lead to less jobs?

Changing Guitar Tuning

Extract lines from files with names begining with given letter

How are hillsides farmed?

How to play a devious character when you are not personally devious?

Is there a high level reason why the inverse square law of gravitation yields periodic orbits without precession?

How can I get something for my shares in a company I used to be employed by?

Is Jupiter bright enough to be seen in color by the naked eye from Jupiter orbit?

Why do the Romance languages use definite articles, when Latin doesn't?

What elements would be created in a star composed entirely of gold?

Functions where the sum of its partial derivatives is zero

Can I create a one way symlink?

Is the EU Settlement Scheme legal?

Company asks (more than once) if I can involve family members in project

Is there any physical evidence for motion?

How to answer to the "We do not want to create any precedent" argument in salary negotiation?

How do I search for a package?

Do European politicians typically put their pronouns on their social media pages?

Do you celebrate paying your mortgage off with colleagues?

What would you do? Different results than what is reported

How to delete music as it's being played

Covering an 8x8 grid with X pentominoes

How can I increase the rate of regeneration in humans without the possibility of tumors developing?

Which Datasheet applies to the ATmega328p?



How should I use the new static option for @ViewChild in Angular 8?


How to use canvas in Angular2?Angular Material Table Sorting - Data Doesn't sort when inside a *ngIf conditionHow we set focus on input element in angular7Angular input interpolationHow do I use ViewChild for dynamic created html elements in Angular 2+?Cannot set property of undefined (when it is defined) in AngularHow can I check which version of Angular I'm using?How can I get new selection in “select” in Angular 2?In Angular, how do you determine the active route?Angular 2 @ViewChild annotation returns undefinedWhat are the “spec.ts” files generated by Angular CLi for?How to bundle an Angular app for productionAngular/RxJs When should I unsubscribe from `Subscription`How to detect when an @Input() value changes in Angular?How to generate components in a specific folder with angular-cli?instantiate @ViewChild on AfterViewInit angular 2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









96

















How should I configure the new Angular 8 view child?



@ViewChild('searchText', read: ElementRef, static: false)
public searchTextInput: ElementRef;


vs



@ViewChild('searchText', read: ElementRef, static: true)
public searchTextInput: ElementRef;


Which is better? When should I use static:true vs static:false?










share|improve this question


































    96

















    How should I configure the new Angular 8 view child?



    @ViewChild('searchText', read: ElementRef, static: false)
    public searchTextInput: ElementRef;


    vs



    @ViewChild('searchText', read: ElementRef, static: true)
    public searchTextInput: ElementRef;


    Which is better? When should I use static:true vs static:false?










    share|improve this question






























      96












      96








      96


      21






      How should I configure the new Angular 8 view child?



      @ViewChild('searchText', read: ElementRef, static: false)
      public searchTextInput: ElementRef;


      vs



      @ViewChild('searchText', read: ElementRef, static: true)
      public searchTextInput: ElementRef;


      Which is better? When should I use static:true vs static:false?










      share|improve this question

















      How should I configure the new Angular 8 view child?



      @ViewChild('searchText', read: ElementRef, static: false)
      public searchTextInput: ElementRef;


      vs



      @ViewChild('searchText', read: ElementRef, static: true)
      public searchTextInput: ElementRef;


      Which is better? When should I use static:true vs static:false?







      angular typescript viewchild angular8






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 31 at 10:38









      Rohit Sharma

      2,6872 gold badges13 silver badges28 bronze badges




      2,6872 gold badges13 silver badges28 bronze badges










      asked May 29 at 11:25









      Patrik LaszloPatrik Laszlo

      1,0063 gold badges12 silver badges23 bronze badges




      1,0063 gold badges12 silver badges23 bronze badges

























          4 Answers
          4






          active

          oldest

          votes


















          102


















          In most cases you will want to use static: false. Setting it like this will ensure query matches that are dependent on binding resolution (like structural directives *ngIf, etc...) will be found.



          Example of when to use static: false:



          @Component(
          template: `
          <div *ngIf="showMe" #viewMe>Am I here?</div>
          <button (click)="showMe = !showMe"></button>
          `
          )
          export class ExampleComponent
          @ViewChild('viewMe', static: false )
          viewMe?: ElementRef<HTMLElement>;

          showMe = false;



          The static: false is going to be the default fallback behaviour in Angular 9. Read more here and here



          The static: true option was introduced to support creating embedded views on the fly. When you are creating a view dynamically and want to acces the TemplateRef, you won't be able to do so in ngAfterViewInit as it will cause a ExpressionHasChangedAfterChecked error. Setting the static flag to true will create your view in ngOnInit.



          Nevertheless:




          In most other cases, the best practice is to use static: false.




          Be aware though that the static: false option will be made default in Angular 9. Which means that setting the static flag is no longer necessary, unless you want to use the static: true option.



          You can use the angular cli ng update command to automatically upgrade your current code base.



          For a migration guide and even more information about this, you can check here






          share|improve this answer




























          • Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

            – Sachin Gupta
            May 29 at 12:04











          • I am not able to access the instance of childView. It says undefined all the time.

            – Nesan Mano
            Jun 3 at 21:52











          • Can you please provide a link about that information of removing the static option in Angular 9?

            – Alex Marinov
            Jun 4 at 13:24











          • @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

            – PierreDuc
            Jun 4 at 13:28











          • @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

            – Andrei Gătej
            Jul 11 at 19:41


















          23


















          So as a rule of thumb you can go for the following:



          • static: true needs to be set when you want to access the ViewChild in ngOnInit.


          • static: false can only be accessed in ngAfterViewInit. This is also what you want to go for when you have a structural directive (i.e. *ngIf) on your element in your template.






          share|improve this answer

































            15


















            From the angular docs




            static - whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.




            It may be a better idea to use static:true if the child does not depend on any conditions. If the visibility of element changes, then static:false may give better results.



            PS: Since its a new feature, we may need to run benchmarks for performance.



            Edit



            As mentioned by @Massimiliano Sartoretto , github commit may give you more insights.






            share|improve this answer























            • 3





              I would add the official motivations behind this feature github.com/angular/angular/pull/28810

              – Massimiliano Sartoretto
              May 29 at 11:35


















            1


















            Came here because a ViewChild was null in ngOnInit after upgrading to Angular 8.



            Static queries are filled before ngOnInit, while dynamic queries (static: false) are filled after. In other words, if a viewchild is now null in ngOnInit after you set static: false, you should consider changing to static: true or move code to ngAfterViewInit.



            See https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L332-L336



            The other answers are correct and explain why this is the case: Queries dependant on structural directives, e.g. a ViewChild reference inside an ngIf, should run after the conditional of this directive has been resolved, ie after change detection. However, one may safely use static: true and thus resolve the queries before ngOnInit for unnested references. Imho this particular case bears mentioning as a null exception could likely be the first way you'll encounter this particularity, as it was for me.






            share|improve this answer





























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );














              draft saved

              draft discarded
















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56359504%2fhow-should-i-use-the-new-static-option-for-viewchild-in-angular-8%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown


























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              102


















              In most cases you will want to use static: false. Setting it like this will ensure query matches that are dependent on binding resolution (like structural directives *ngIf, etc...) will be found.



              Example of when to use static: false:



              @Component(
              template: `
              <div *ngIf="showMe" #viewMe>Am I here?</div>
              <button (click)="showMe = !showMe"></button>
              `
              )
              export class ExampleComponent
              @ViewChild('viewMe', static: false )
              viewMe?: ElementRef<HTMLElement>;

              showMe = false;



              The static: false is going to be the default fallback behaviour in Angular 9. Read more here and here



              The static: true option was introduced to support creating embedded views on the fly. When you are creating a view dynamically and want to acces the TemplateRef, you won't be able to do so in ngAfterViewInit as it will cause a ExpressionHasChangedAfterChecked error. Setting the static flag to true will create your view in ngOnInit.



              Nevertheless:




              In most other cases, the best practice is to use static: false.




              Be aware though that the static: false option will be made default in Angular 9. Which means that setting the static flag is no longer necessary, unless you want to use the static: true option.



              You can use the angular cli ng update command to automatically upgrade your current code base.



              For a migration guide and even more information about this, you can check here






              share|improve this answer




























              • Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

                – Sachin Gupta
                May 29 at 12:04











              • I am not able to access the instance of childView. It says undefined all the time.

                – Nesan Mano
                Jun 3 at 21:52











              • Can you please provide a link about that information of removing the static option in Angular 9?

                – Alex Marinov
                Jun 4 at 13:24











              • @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

                – PierreDuc
                Jun 4 at 13:28











              • @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

                – Andrei Gătej
                Jul 11 at 19:41















              102


















              In most cases you will want to use static: false. Setting it like this will ensure query matches that are dependent on binding resolution (like structural directives *ngIf, etc...) will be found.



              Example of when to use static: false:



              @Component(
              template: `
              <div *ngIf="showMe" #viewMe>Am I here?</div>
              <button (click)="showMe = !showMe"></button>
              `
              )
              export class ExampleComponent
              @ViewChild('viewMe', static: false )
              viewMe?: ElementRef<HTMLElement>;

              showMe = false;



              The static: false is going to be the default fallback behaviour in Angular 9. Read more here and here



              The static: true option was introduced to support creating embedded views on the fly. When you are creating a view dynamically and want to acces the TemplateRef, you won't be able to do so in ngAfterViewInit as it will cause a ExpressionHasChangedAfterChecked error. Setting the static flag to true will create your view in ngOnInit.



              Nevertheless:




              In most other cases, the best practice is to use static: false.




              Be aware though that the static: false option will be made default in Angular 9. Which means that setting the static flag is no longer necessary, unless you want to use the static: true option.



              You can use the angular cli ng update command to automatically upgrade your current code base.



              For a migration guide and even more information about this, you can check here






              share|improve this answer




























              • Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

                – Sachin Gupta
                May 29 at 12:04











              • I am not able to access the instance of childView. It says undefined all the time.

                – Nesan Mano
                Jun 3 at 21:52











              • Can you please provide a link about that information of removing the static option in Angular 9?

                – Alex Marinov
                Jun 4 at 13:24











              • @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

                – PierreDuc
                Jun 4 at 13:28











              • @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

                – Andrei Gătej
                Jul 11 at 19:41













              102














              102










              102









              In most cases you will want to use static: false. Setting it like this will ensure query matches that are dependent on binding resolution (like structural directives *ngIf, etc...) will be found.



              Example of when to use static: false:



              @Component(
              template: `
              <div *ngIf="showMe" #viewMe>Am I here?</div>
              <button (click)="showMe = !showMe"></button>
              `
              )
              export class ExampleComponent
              @ViewChild('viewMe', static: false )
              viewMe?: ElementRef<HTMLElement>;

              showMe = false;



              The static: false is going to be the default fallback behaviour in Angular 9. Read more here and here



              The static: true option was introduced to support creating embedded views on the fly. When you are creating a view dynamically and want to acces the TemplateRef, you won't be able to do so in ngAfterViewInit as it will cause a ExpressionHasChangedAfterChecked error. Setting the static flag to true will create your view in ngOnInit.



              Nevertheless:




              In most other cases, the best practice is to use static: false.




              Be aware though that the static: false option will be made default in Angular 9. Which means that setting the static flag is no longer necessary, unless you want to use the static: true option.



              You can use the angular cli ng update command to automatically upgrade your current code base.



              For a migration guide and even more information about this, you can check here






              share|improve this answer
















              In most cases you will want to use static: false. Setting it like this will ensure query matches that are dependent on binding resolution (like structural directives *ngIf, etc...) will be found.



              Example of when to use static: false:



              @Component(
              template: `
              <div *ngIf="showMe" #viewMe>Am I here?</div>
              <button (click)="showMe = !showMe"></button>
              `
              )
              export class ExampleComponent
              @ViewChild('viewMe', static: false )
              viewMe?: ElementRef<HTMLElement>;

              showMe = false;



              The static: false is going to be the default fallback behaviour in Angular 9. Read more here and here



              The static: true option was introduced to support creating embedded views on the fly. When you are creating a view dynamically and want to acces the TemplateRef, you won't be able to do so in ngAfterViewInit as it will cause a ExpressionHasChangedAfterChecked error. Setting the static flag to true will create your view in ngOnInit.



              Nevertheless:




              In most other cases, the best practice is to use static: false.




              Be aware though that the static: false option will be made default in Angular 9. Which means that setting the static flag is no longer necessary, unless you want to use the static: true option.



              You can use the angular cli ng update command to automatically upgrade your current code base.



              For a migration guide and even more information about this, you can check here







              share|improve this answer















              share|improve this answer




              share|improve this answer








              edited Jun 4 at 13:28

























              answered May 29 at 11:31









              PierreDucPierreDuc

              37.2k6 gold badges72 silver badges90 bronze badges




              37.2k6 gold badges72 silver badges90 bronze badges















              • Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

                – Sachin Gupta
                May 29 at 12:04











              • I am not able to access the instance of childView. It says undefined all the time.

                – Nesan Mano
                Jun 3 at 21:52











              • Can you please provide a link about that information of removing the static option in Angular 9?

                – Alex Marinov
                Jun 4 at 13:24











              • @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

                – PierreDuc
                Jun 4 at 13:28











              • @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

                – Andrei Gătej
                Jul 11 at 19:41

















              • Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

                – Sachin Gupta
                May 29 at 12:04











              • I am not able to access the instance of childView. It says undefined all the time.

                – Nesan Mano
                Jun 3 at 21:52











              • Can you please provide a link about that information of removing the static option in Angular 9?

                – Alex Marinov
                Jun 4 at 13:24











              • @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

                – PierreDuc
                Jun 4 at 13:28











              • @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

                – Andrei Gătej
                Jul 11 at 19:41
















              Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

              – Sachin Gupta
              May 29 at 12:04





              Please update the link for angular docs (changed after release) angular.io/api/core/ViewChild#description

              – Sachin Gupta
              May 29 at 12:04













              I am not able to access the instance of childView. It says undefined all the time.

              – Nesan Mano
              Jun 3 at 21:52





              I am not able to access the instance of childView. It says undefined all the time.

              – Nesan Mano
              Jun 3 at 21:52













              Can you please provide a link about that information of removing the static option in Angular 9?

              – Alex Marinov
              Jun 4 at 13:24





              Can you please provide a link about that information of removing the static option in Angular 9?

              – Alex Marinov
              Jun 4 at 13:24













              @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

              – PierreDuc
              Jun 4 at 13:28





              @AlexMarinov I've updated my answer to make it more clear what's going to happen in angular 9. The link about this is in the migration guide

              – PierreDuc
              Jun 4 at 13:28













              @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

              – Andrei Gătej
              Jul 11 at 19:41





              @NesanMano I run into the same problem today, the way I solved was to set static: true . Hope this helps

              – Andrei Gătej
              Jul 11 at 19:41













              23


















              So as a rule of thumb you can go for the following:



              • static: true needs to be set when you want to access the ViewChild in ngOnInit.


              • static: false can only be accessed in ngAfterViewInit. This is also what you want to go for when you have a structural directive (i.e. *ngIf) on your element in your template.






              share|improve this answer






























                23


















                So as a rule of thumb you can go for the following:



                • static: true needs to be set when you want to access the ViewChild in ngOnInit.


                • static: false can only be accessed in ngAfterViewInit. This is also what you want to go for when you have a structural directive (i.e. *ngIf) on your element in your template.






                share|improve this answer




























                  23














                  23










                  23









                  So as a rule of thumb you can go for the following:



                  • static: true needs to be set when you want to access the ViewChild in ngOnInit.


                  • static: false can only be accessed in ngAfterViewInit. This is also what you want to go for when you have a structural directive (i.e. *ngIf) on your element in your template.






                  share|improve this answer














                  So as a rule of thumb you can go for the following:



                  • static: true needs to be set when you want to access the ViewChild in ngOnInit.


                  • static: false can only be accessed in ngAfterViewInit. This is also what you want to go for when you have a structural directive (i.e. *ngIf) on your element in your template.







                  share|improve this answer













                  share|improve this answer




                  share|improve this answer










                  answered Jul 18 at 7:50









                  dave0688dave0688

                  1,4731 gold badge10 silver badges37 bronze badges




                  1,4731 gold badge10 silver badges37 bronze badges
























                      15


















                      From the angular docs




                      static - whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.




                      It may be a better idea to use static:true if the child does not depend on any conditions. If the visibility of element changes, then static:false may give better results.



                      PS: Since its a new feature, we may need to run benchmarks for performance.



                      Edit



                      As mentioned by @Massimiliano Sartoretto , github commit may give you more insights.






                      share|improve this answer























                      • 3





                        I would add the official motivations behind this feature github.com/angular/angular/pull/28810

                        – Massimiliano Sartoretto
                        May 29 at 11:35















                      15


















                      From the angular docs




                      static - whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.




                      It may be a better idea to use static:true if the child does not depend on any conditions. If the visibility of element changes, then static:false may give better results.



                      PS: Since its a new feature, we may need to run benchmarks for performance.



                      Edit



                      As mentioned by @Massimiliano Sartoretto , github commit may give you more insights.






                      share|improve this answer























                      • 3





                        I would add the official motivations behind this feature github.com/angular/angular/pull/28810

                        – Massimiliano Sartoretto
                        May 29 at 11:35













                      15














                      15










                      15









                      From the angular docs




                      static - whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.




                      It may be a better idea to use static:true if the child does not depend on any conditions. If the visibility of element changes, then static:false may give better results.



                      PS: Since its a new feature, we may need to run benchmarks for performance.



                      Edit



                      As mentioned by @Massimiliano Sartoretto , github commit may give you more insights.






                      share|improve this answer
















                      From the angular docs




                      static - whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.




                      It may be a better idea to use static:true if the child does not depend on any conditions. If the visibility of element changes, then static:false may give better results.



                      PS: Since its a new feature, we may need to run benchmarks for performance.



                      Edit



                      As mentioned by @Massimiliano Sartoretto , github commit may give you more insights.







                      share|improve this answer















                      share|improve this answer




                      share|improve this answer








                      edited May 29 at 12:02

























                      answered May 29 at 11:33









                      Sachin GuptaSachin Gupta

                      2,3082 gold badges6 silver badges22 bronze badges




                      2,3082 gold badges6 silver badges22 bronze badges










                      • 3





                        I would add the official motivations behind this feature github.com/angular/angular/pull/28810

                        – Massimiliano Sartoretto
                        May 29 at 11:35












                      • 3





                        I would add the official motivations behind this feature github.com/angular/angular/pull/28810

                        – Massimiliano Sartoretto
                        May 29 at 11:35







                      3




                      3





                      I would add the official motivations behind this feature github.com/angular/angular/pull/28810

                      – Massimiliano Sartoretto
                      May 29 at 11:35





                      I would add the official motivations behind this feature github.com/angular/angular/pull/28810

                      – Massimiliano Sartoretto
                      May 29 at 11:35











                      1


















                      Came here because a ViewChild was null in ngOnInit after upgrading to Angular 8.



                      Static queries are filled before ngOnInit, while dynamic queries (static: false) are filled after. In other words, if a viewchild is now null in ngOnInit after you set static: false, you should consider changing to static: true or move code to ngAfterViewInit.



                      See https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L332-L336



                      The other answers are correct and explain why this is the case: Queries dependant on structural directives, e.g. a ViewChild reference inside an ngIf, should run after the conditional of this directive has been resolved, ie after change detection. However, one may safely use static: true and thus resolve the queries before ngOnInit for unnested references. Imho this particular case bears mentioning as a null exception could likely be the first way you'll encounter this particularity, as it was for me.






                      share|improve this answer
































                        1


















                        Came here because a ViewChild was null in ngOnInit after upgrading to Angular 8.



                        Static queries are filled before ngOnInit, while dynamic queries (static: false) are filled after. In other words, if a viewchild is now null in ngOnInit after you set static: false, you should consider changing to static: true or move code to ngAfterViewInit.



                        See https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L332-L336



                        The other answers are correct and explain why this is the case: Queries dependant on structural directives, e.g. a ViewChild reference inside an ngIf, should run after the conditional of this directive has been resolved, ie after change detection. However, one may safely use static: true and thus resolve the queries before ngOnInit for unnested references. Imho this particular case bears mentioning as a null exception could likely be the first way you'll encounter this particularity, as it was for me.






                        share|improve this answer






























                          1














                          1










                          1









                          Came here because a ViewChild was null in ngOnInit after upgrading to Angular 8.



                          Static queries are filled before ngOnInit, while dynamic queries (static: false) are filled after. In other words, if a viewchild is now null in ngOnInit after you set static: false, you should consider changing to static: true or move code to ngAfterViewInit.



                          See https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L332-L336



                          The other answers are correct and explain why this is the case: Queries dependant on structural directives, e.g. a ViewChild reference inside an ngIf, should run after the conditional of this directive has been resolved, ie after change detection. However, one may safely use static: true and thus resolve the queries before ngOnInit for unnested references. Imho this particular case bears mentioning as a null exception could likely be the first way you'll encounter this particularity, as it was for me.






                          share|improve this answer
















                          Came here because a ViewChild was null in ngOnInit after upgrading to Angular 8.



                          Static queries are filled before ngOnInit, while dynamic queries (static: false) are filled after. In other words, if a viewchild is now null in ngOnInit after you set static: false, you should consider changing to static: true or move code to ngAfterViewInit.



                          See https://github.com/angular/angular/blob/master/packages/core/src/view/view.ts#L332-L336



                          The other answers are correct and explain why this is the case: Queries dependant on structural directives, e.g. a ViewChild reference inside an ngIf, should run after the conditional of this directive has been resolved, ie after change detection. However, one may safely use static: true and thus resolve the queries before ngOnInit for unnested references. Imho this particular case bears mentioning as a null exception could likely be the first way you'll encounter this particularity, as it was for me.







                          share|improve this answer















                          share|improve this answer




                          share|improve this answer








                          edited Jul 10 at 7:19

























                          answered Jul 10 at 7:04









                          corollacorolla

                          3,8081 gold badge16 silver badges16 bronze badges




                          3,8081 gold badge16 silver badges16 bronze badges































                              draft saved

                              draft discarded















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56359504%2fhow-should-i-use-the-new-static-option-for-viewchild-in-angular-8%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown









                              Popular posts from this blog

                              Tamil (spriik) Luke uk diar | Nawigatjuun

                              Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                              Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?