How to target an element in the template loaded based on an *ngIf conditionHow to Vertical align elements in a div?How to tell if a DOM element is visible in the current viewport?How do you keep parents of floated elements from collapsing?How can I select an element by name with jQuery?How to move an element into another element?How can I set the default value for an HTML <select> element?How do I remove the space between inline-block elements?How can I select an element in a component template?*ngIf and *ngFor on same element causing errorHow to use *ngIf else?

Map extent same as grid for QGIS Atlas

How to make a language evolve quickly?

Why are low spin tetrahedral complexes so rare?

How to compare d20+x with advantage to d20+y without advantage (x < y)

Would encrypting a database protect against a compromised admin account?

What is the name of meteoroids which hit Moon, Mars, or pretty much anything that isn’t the Earth?

Cryptography and elliptic curves

What is wrong with my code? RGB potentiometer

Is every story set in the future "science fiction"?

How to handle DM constantly stealing everything from sleeping characters?

Hexagonal Grid Filling

Need help replacing old cassette and chain

Is there any evidence to support the claim that the United States was "suckered into WW1" by Zionists, made by Benjamin Freedman in his 1961 speech

How to slow yourself down (for playing nice with others)

Why is the Sun made of light elements only?

What was the notion of limit that had Newton?

Electric kick drum pedal starts oscillating in such a way that it does not register hits

Why do the Avengers care about returning these items in Endgame?

Removing all characters except digits from clipboard

How to get a ellipse shaped node in Tikz Network?

Is there an application which does HTTP PUT?

Is it a Munchausen Number?

Is there a need for better software for writers?

Why can't I prove summation identities without guessing?



How to target an element in the template loaded based on an *ngIf condition


How to Vertical align elements in a div?How to tell if a DOM element is visible in the current viewport?How do you keep parents of floated elements from collapsing?How can I select an element by name with jQuery?How to move an element into another element?How can I set the default value for an HTML <select> element?How do I remove the space between inline-block elements?How can I select an element in a component template?*ngIf and *ngFor on same element causing errorHow to use *ngIf else?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to target an element in my template through a template variable but it keeps returning undefined.



When you navigate to a certain route in my application, my component fetches some data inside the forkJoin method from rxjs. It's quite a bit of data and I'm on a slow connection so there is a 2 second delay, hence, I have an intermediate state, where I display a loading spinner, which then disappears when all the data comes back.



My template code looks something like...



<div class="container" *ngIf="ready; else loading">
<mat-horizontal-stepper labelPosition="bottom" #stepper>
<mat-step label="Step 1"> Step 1 Content... </mat-step>
<mat-step label="Step 2"> Step 2 Content... </mat-step>
<mat-step label="Step 3"> Step 3 Content... </mat-step>
</mat-horizontal-stepper>
</div>

<ng-template #loading>
<mat-spinner></mat-spinner>
</ng-template>


My component.ts file looks like this...



public ready = false;
public dataset1;
public dataset2;
@ViewChild('stepper') private myStepper: MatStepper;

constructor(private dataService: DataService)

ngOnInit()
this.fetch();
console.log(this.myStepper); // returns undefined


ngAfterViewInit()
console.log(this.myStepper); // also returns undefined


public fetch()
return forkJoin(
this.dataService.getDataSet1(),
this.dataService.getDataset2()
).subscribe(res =>
this.dataset1 = res[0];
this.dataset2 = res[1];
this.ready = true;
, error => console.log('Error : ', error));



I'd like to be able to target the stepper and get the total count of the steps and use those to navigate to a different step based on some user activity but I can't do that if I keep getting undefined.
How might I fix this?










share|improve this question




























    1















    I'm trying to target an element in my template through a template variable but it keeps returning undefined.



    When you navigate to a certain route in my application, my component fetches some data inside the forkJoin method from rxjs. It's quite a bit of data and I'm on a slow connection so there is a 2 second delay, hence, I have an intermediate state, where I display a loading spinner, which then disappears when all the data comes back.



    My template code looks something like...



    <div class="container" *ngIf="ready; else loading">
    <mat-horizontal-stepper labelPosition="bottom" #stepper>
    <mat-step label="Step 1"> Step 1 Content... </mat-step>
    <mat-step label="Step 2"> Step 2 Content... </mat-step>
    <mat-step label="Step 3"> Step 3 Content... </mat-step>
    </mat-horizontal-stepper>
    </div>

    <ng-template #loading>
    <mat-spinner></mat-spinner>
    </ng-template>


    My component.ts file looks like this...



    public ready = false;
    public dataset1;
    public dataset2;
    @ViewChild('stepper') private myStepper: MatStepper;

    constructor(private dataService: DataService)

    ngOnInit()
    this.fetch();
    console.log(this.myStepper); // returns undefined


    ngAfterViewInit()
    console.log(this.myStepper); // also returns undefined


    public fetch()
    return forkJoin(
    this.dataService.getDataSet1(),
    this.dataService.getDataset2()
    ).subscribe(res =>
    this.dataset1 = res[0];
    this.dataset2 = res[1];
    this.ready = true;
    , error => console.log('Error : ', error));



    I'd like to be able to target the stepper and get the total count of the steps and use those to navigate to a different step based on some user activity but I can't do that if I keep getting undefined.
    How might I fix this?










    share|improve this question
























      1












      1








      1


      1






      I'm trying to target an element in my template through a template variable but it keeps returning undefined.



      When you navigate to a certain route in my application, my component fetches some data inside the forkJoin method from rxjs. It's quite a bit of data and I'm on a slow connection so there is a 2 second delay, hence, I have an intermediate state, where I display a loading spinner, which then disappears when all the data comes back.



      My template code looks something like...



      <div class="container" *ngIf="ready; else loading">
      <mat-horizontal-stepper labelPosition="bottom" #stepper>
      <mat-step label="Step 1"> Step 1 Content... </mat-step>
      <mat-step label="Step 2"> Step 2 Content... </mat-step>
      <mat-step label="Step 3"> Step 3 Content... </mat-step>
      </mat-horizontal-stepper>
      </div>

      <ng-template #loading>
      <mat-spinner></mat-spinner>
      </ng-template>


      My component.ts file looks like this...



      public ready = false;
      public dataset1;
      public dataset2;
      @ViewChild('stepper') private myStepper: MatStepper;

      constructor(private dataService: DataService)

      ngOnInit()
      this.fetch();
      console.log(this.myStepper); // returns undefined


      ngAfterViewInit()
      console.log(this.myStepper); // also returns undefined


      public fetch()
      return forkJoin(
      this.dataService.getDataSet1(),
      this.dataService.getDataset2()
      ).subscribe(res =>
      this.dataset1 = res[0];
      this.dataset2 = res[1];
      this.ready = true;
      , error => console.log('Error : ', error));



      I'd like to be able to target the stepper and get the total count of the steps and use those to navigate to a different step based on some user activity but I can't do that if I keep getting undefined.
      How might I fix this?










      share|improve this question














      I'm trying to target an element in my template through a template variable but it keeps returning undefined.



      When you navigate to a certain route in my application, my component fetches some data inside the forkJoin method from rxjs. It's quite a bit of data and I'm on a slow connection so there is a 2 second delay, hence, I have an intermediate state, where I display a loading spinner, which then disappears when all the data comes back.



      My template code looks something like...



      <div class="container" *ngIf="ready; else loading">
      <mat-horizontal-stepper labelPosition="bottom" #stepper>
      <mat-step label="Step 1"> Step 1 Content... </mat-step>
      <mat-step label="Step 2"> Step 2 Content... </mat-step>
      <mat-step label="Step 3"> Step 3 Content... </mat-step>
      </mat-horizontal-stepper>
      </div>

      <ng-template #loading>
      <mat-spinner></mat-spinner>
      </ng-template>


      My component.ts file looks like this...



      public ready = false;
      public dataset1;
      public dataset2;
      @ViewChild('stepper') private myStepper: MatStepper;

      constructor(private dataService: DataService)

      ngOnInit()
      this.fetch();
      console.log(this.myStepper); // returns undefined


      ngAfterViewInit()
      console.log(this.myStepper); // also returns undefined


      public fetch()
      return forkJoin(
      this.dataService.getDataSet1(),
      this.dataService.getDataset2()
      ).subscribe(res =>
      this.dataset1 = res[0];
      this.dataset2 = res[1];
      this.ready = true;
      , error => console.log('Error : ', error));



      I'd like to be able to target the stepper and get the total count of the steps and use those to navigate to a different step based on some user activity but I can't do that if I keep getting undefined.
      How might I fix this?







      html angular






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 9:33









      Nelson KingNelson King

      1099




      1099






















          1 Answer
          1






          active

          oldest

          votes


















          3














          Because you have it inside a *ngIf block, you will not able to access it until whatever he *ngIf is conditional to is true as this directive does not load the html elements until it is true, including your stepper. This means you could check for the value where I have added it in the snippet below. As at this point you set your ready variable to true meaning that section of the html will also be available. This might be a good point to call a function to run your logic.



          As mentioned in the comment it would be ideal to call the change detection before trying to access. This can be done by injecting ChangeDetectorRef through the constructor.



          import ChangeDetectorRef from '@angular/core';


          Make sure your imports above include, ChangeDetectorRef.



          constructor(private ref: ChangeDetectorRef,
          private dataService: DataService)

          public fetch() {
          return forkJoin(
          this.dataService.getDataSet1(),
          this.dataService.getDataset2()
          ).subscribe(res =>
          this.dataset1 = res[0];
          this.dataset2 = res[1];
          this.ready = true;

          this.ref.detectChanges();
          console.log(this.stepper); // you will be able to read it here.

          , error => console.log('Error : ', error));


          This article helps explain the dom behaviour with *ngIf.



          Something else to take into account would be using a setter for the ViewChild, this means the value would only be set when the *ngIf is true. Meaning it would be available as soon as it has been rendered in the dom. This is a good way to get the reference and would look like. This will also be initially set with undefined until change detection has run or been manually run.



          public myStepper: MatStepper;

          @ViewChild('stepper') set content(content: MatStepper)
          this.myStepper = content;



          That way you can reference your ViewChild and interact with it through this.myStepper.






          share|improve this answer




















          • 1





            I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

            – yurzui
            Mar 23 at 10:23






          • 1





            Good call, lets force the change detection, will add it in :)

            – dince12
            Mar 23 at 10:24











          • Also there is a way to catch that reference by using setter on ViewChild

            – yurzui
            Mar 23 at 10:34











          • added a setter for catching reference :-)

            – dince12
            Mar 23 at 10:43











          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/3.0/"u003ecc by-sa 3.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%2f55312394%2fhow-to-target-an-element-in-the-template-loaded-based-on-an-ngif-condition%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Because you have it inside a *ngIf block, you will not able to access it until whatever he *ngIf is conditional to is true as this directive does not load the html elements until it is true, including your stepper. This means you could check for the value where I have added it in the snippet below. As at this point you set your ready variable to true meaning that section of the html will also be available. This might be a good point to call a function to run your logic.



          As mentioned in the comment it would be ideal to call the change detection before trying to access. This can be done by injecting ChangeDetectorRef through the constructor.



          import ChangeDetectorRef from '@angular/core';


          Make sure your imports above include, ChangeDetectorRef.



          constructor(private ref: ChangeDetectorRef,
          private dataService: DataService)

          public fetch() {
          return forkJoin(
          this.dataService.getDataSet1(),
          this.dataService.getDataset2()
          ).subscribe(res =>
          this.dataset1 = res[0];
          this.dataset2 = res[1];
          this.ready = true;

          this.ref.detectChanges();
          console.log(this.stepper); // you will be able to read it here.

          , error => console.log('Error : ', error));


          This article helps explain the dom behaviour with *ngIf.



          Something else to take into account would be using a setter for the ViewChild, this means the value would only be set when the *ngIf is true. Meaning it would be available as soon as it has been rendered in the dom. This is a good way to get the reference and would look like. This will also be initially set with undefined until change detection has run or been manually run.



          public myStepper: MatStepper;

          @ViewChild('stepper') set content(content: MatStepper)
          this.myStepper = content;



          That way you can reference your ViewChild and interact with it through this.myStepper.






          share|improve this answer




















          • 1





            I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

            – yurzui
            Mar 23 at 10:23






          • 1





            Good call, lets force the change detection, will add it in :)

            – dince12
            Mar 23 at 10:24











          • Also there is a way to catch that reference by using setter on ViewChild

            – yurzui
            Mar 23 at 10:34











          • added a setter for catching reference :-)

            – dince12
            Mar 23 at 10:43















          3














          Because you have it inside a *ngIf block, you will not able to access it until whatever he *ngIf is conditional to is true as this directive does not load the html elements until it is true, including your stepper. This means you could check for the value where I have added it in the snippet below. As at this point you set your ready variable to true meaning that section of the html will also be available. This might be a good point to call a function to run your logic.



          As mentioned in the comment it would be ideal to call the change detection before trying to access. This can be done by injecting ChangeDetectorRef through the constructor.



          import ChangeDetectorRef from '@angular/core';


          Make sure your imports above include, ChangeDetectorRef.



          constructor(private ref: ChangeDetectorRef,
          private dataService: DataService)

          public fetch() {
          return forkJoin(
          this.dataService.getDataSet1(),
          this.dataService.getDataset2()
          ).subscribe(res =>
          this.dataset1 = res[0];
          this.dataset2 = res[1];
          this.ready = true;

          this.ref.detectChanges();
          console.log(this.stepper); // you will be able to read it here.

          , error => console.log('Error : ', error));


          This article helps explain the dom behaviour with *ngIf.



          Something else to take into account would be using a setter for the ViewChild, this means the value would only be set when the *ngIf is true. Meaning it would be available as soon as it has been rendered in the dom. This is a good way to get the reference and would look like. This will also be initially set with undefined until change detection has run or been manually run.



          public myStepper: MatStepper;

          @ViewChild('stepper') set content(content: MatStepper)
          this.myStepper = content;



          That way you can reference your ViewChild and interact with it through this.myStepper.






          share|improve this answer




















          • 1





            I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

            – yurzui
            Mar 23 at 10:23






          • 1





            Good call, lets force the change detection, will add it in :)

            – dince12
            Mar 23 at 10:24











          • Also there is a way to catch that reference by using setter on ViewChild

            – yurzui
            Mar 23 at 10:34











          • added a setter for catching reference :-)

            – dince12
            Mar 23 at 10:43













          3












          3








          3







          Because you have it inside a *ngIf block, you will not able to access it until whatever he *ngIf is conditional to is true as this directive does not load the html elements until it is true, including your stepper. This means you could check for the value where I have added it in the snippet below. As at this point you set your ready variable to true meaning that section of the html will also be available. This might be a good point to call a function to run your logic.



          As mentioned in the comment it would be ideal to call the change detection before trying to access. This can be done by injecting ChangeDetectorRef through the constructor.



          import ChangeDetectorRef from '@angular/core';


          Make sure your imports above include, ChangeDetectorRef.



          constructor(private ref: ChangeDetectorRef,
          private dataService: DataService)

          public fetch() {
          return forkJoin(
          this.dataService.getDataSet1(),
          this.dataService.getDataset2()
          ).subscribe(res =>
          this.dataset1 = res[0];
          this.dataset2 = res[1];
          this.ready = true;

          this.ref.detectChanges();
          console.log(this.stepper); // you will be able to read it here.

          , error => console.log('Error : ', error));


          This article helps explain the dom behaviour with *ngIf.



          Something else to take into account would be using a setter for the ViewChild, this means the value would only be set when the *ngIf is true. Meaning it would be available as soon as it has been rendered in the dom. This is a good way to get the reference and would look like. This will also be initially set with undefined until change detection has run or been manually run.



          public myStepper: MatStepper;

          @ViewChild('stepper') set content(content: MatStepper)
          this.myStepper = content;



          That way you can reference your ViewChild and interact with it through this.myStepper.






          share|improve this answer















          Because you have it inside a *ngIf block, you will not able to access it until whatever he *ngIf is conditional to is true as this directive does not load the html elements until it is true, including your stepper. This means you could check for the value where I have added it in the snippet below. As at this point you set your ready variable to true meaning that section of the html will also be available. This might be a good point to call a function to run your logic.



          As mentioned in the comment it would be ideal to call the change detection before trying to access. This can be done by injecting ChangeDetectorRef through the constructor.



          import ChangeDetectorRef from '@angular/core';


          Make sure your imports above include, ChangeDetectorRef.



          constructor(private ref: ChangeDetectorRef,
          private dataService: DataService)

          public fetch() {
          return forkJoin(
          this.dataService.getDataSet1(),
          this.dataService.getDataset2()
          ).subscribe(res =>
          this.dataset1 = res[0];
          this.dataset2 = res[1];
          this.ready = true;

          this.ref.detectChanges();
          console.log(this.stepper); // you will be able to read it here.

          , error => console.log('Error : ', error));


          This article helps explain the dom behaviour with *ngIf.



          Something else to take into account would be using a setter for the ViewChild, this means the value would only be set when the *ngIf is true. Meaning it would be available as soon as it has been rendered in the dom. This is a good way to get the reference and would look like. This will also be initially set with undefined until change detection has run or been manually run.



          public myStepper: MatStepper;

          @ViewChild('stepper') set content(content: MatStepper)
          this.myStepper = content;



          That way you can reference your ViewChild and interact with it through this.myStepper.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 at 22:00

























          answered Mar 23 at 9:58









          dince12dince12

          2,4113723




          2,4113723







          • 1





            I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

            – yurzui
            Mar 23 at 10:23






          • 1





            Good call, lets force the change detection, will add it in :)

            – dince12
            Mar 23 at 10:24











          • Also there is a way to catch that reference by using setter on ViewChild

            – yurzui
            Mar 23 at 10:34











          • added a setter for catching reference :-)

            – dince12
            Mar 23 at 10:43












          • 1





            I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

            – yurzui
            Mar 23 at 10:23






          • 1





            Good call, lets force the change detection, will add it in :)

            – dince12
            Mar 23 at 10:24











          • Also there is a way to catch that reference by using setter on ViewChild

            – yurzui
            Mar 23 at 10:34











          • added a setter for catching reference :-)

            – dince12
            Mar 23 at 10:43







          1




          1





          I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

          – yurzui
          Mar 23 at 10:23





          I suspect I will still unavailable at that point unless you call cdRef.detectChanges() after this.ready = true; line

          – yurzui
          Mar 23 at 10:23




          1




          1





          Good call, lets force the change detection, will add it in :)

          – dince12
          Mar 23 at 10:24





          Good call, lets force the change detection, will add it in :)

          – dince12
          Mar 23 at 10:24













          Also there is a way to catch that reference by using setter on ViewChild

          – yurzui
          Mar 23 at 10:34





          Also there is a way to catch that reference by using setter on ViewChild

          – yurzui
          Mar 23 at 10:34













          added a setter for catching reference :-)

          – dince12
          Mar 23 at 10:43





          added a setter for catching reference :-)

          – dince12
          Mar 23 at 10:43



















          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%2f55312394%2fhow-to-target-an-element-in-the-template-loaded-based-on-an-ngif-condition%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript