@ViewChild returns undefined - Angular 22019 Community Moderator ElectionAngular HTML bindingAngular 2 @ViewChild annotation returns undefinedAngular 2 @ViewChild returns undefinedGet incorrect offsetWidth and offsetHeight valuesHow to get offsetWidth and offsetHeight values after add css class to change themUpdating a user list when a user is created in Angular 2@ViewChild returns undefinedHow can I access DOM elements in angularViewChild returns 'undefined' - Angular2Placeholder in mat-autoComplete does not work as illustrated in the Angular Material Documentation

One circle's diameter is different from others within a series of circles

What do you call someone who likes to pick fights?

Smooth vector fields on a surface modulo diffeomorphisms

Numerical value of Determinant far from what it is supposed to be

Difference between `nmap local-IP-address` and `nmap localhost`

What does *dead* mean in *What do you mean, dead?*?

ESPP--any reason not to go all in?

Giving a career talk in my old university, how prominently should I tell students my salary?

Will expression retain the same definition if particle is changed?

Why aren't there more Gauls like Obelix?

Was it really inappropriate to write a pull request for the company I interviewed with?

What will happen if my luggage gets delayed?

Create chunks from an array

I can't die. Who am I?

Can one live in the U.S. and not use a credit card?

Does an unused member variable take up memory?

Origin of the word “pushka”

If nine coins are tossed, what is the probability that the number of heads is even?

Having the player face themselves after the mid-game

What is the purpose of a disclaimer like "this is not legal advice"?

Can't make sense of a paragraph from Lovecraft

How should I solve this integral with changing parameters?

Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?

Why restrict private health insurance?



@ViewChild returns undefined - Angular 2



2019 Community Moderator ElectionAngular HTML bindingAngular 2 @ViewChild annotation returns undefinedAngular 2 @ViewChild returns undefinedGet incorrect offsetWidth and offsetHeight valuesHow to get offsetWidth and offsetHeight values after add css class to change themUpdating a user list when a user is created in Angular 2@ViewChild returns undefinedHow can I access DOM elements in angularViewChild returns 'undefined' - Angular2Placeholder in mat-autoComplete does not work as illustrated in the Angular Material Documentation










-2















I'm trying to use @ViewChild to get the DOM element that I need. I have the following component that describes my problem:



import Component, ViewChild, ElementRef, OnInit, Input from "@angular/core";


@Component(
selector: 'some-comp',
template: `

<input
#myInputOut
type="text"
class="google-place-input"
placeholder="Type to search..">

<span class="row form-group">
<required-input
class="col-12 has-danger"
[label]="'somel:'"
[controlName]="'somel'"
[form]="group"
</required-input>
</span>

<div class="row form-group2 required">
<label class=" col-3 control-label" for="street">label:</label>
<input #myInputIn class="col-7" id="someid" placeholder="Type to search.." /></div>
`
)
export class someClass implements OnInit

@ViewChild('myInputOut') myInputOut: ElementRef;
@ViewChild('myInputIn') myInputIn: ElementRef;


private element: HTMLInputElement;
private element2: HTMLInputElement;

constructor( private databaseService : DatabaseService,
private router : Router)




ngOnInit()
this.element = this.myInputOut.nativeElement;
this.element2 = this.myInputIn.nativeElement;




For some reason myInputOut returns properly but myInputIn returns undefined.
How can I solve this?










share|improve this question









New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    try to remove comment opening <!-- before <div class="row form-group2 required">

    – Andriy
    Mar 6 at 13:56











  • you are not closing required-input correctly .. try to close it

    – Mohamed Ali RACHID
    Mar 6 at 13:58












  • @Andriy That was the problem, thanks.

    – Zak
    Mar 6 at 14:23











  • Please make sure the code that you post here is well formatted and syntactically right.

    – nircraft
    Mar 6 at 14:24















-2















I'm trying to use @ViewChild to get the DOM element that I need. I have the following component that describes my problem:



import Component, ViewChild, ElementRef, OnInit, Input from "@angular/core";


@Component(
selector: 'some-comp',
template: `

<input
#myInputOut
type="text"
class="google-place-input"
placeholder="Type to search..">

<span class="row form-group">
<required-input
class="col-12 has-danger"
[label]="'somel:'"
[controlName]="'somel'"
[form]="group"
</required-input>
</span>

<div class="row form-group2 required">
<label class=" col-3 control-label" for="street">label:</label>
<input #myInputIn class="col-7" id="someid" placeholder="Type to search.." /></div>
`
)
export class someClass implements OnInit

@ViewChild('myInputOut') myInputOut: ElementRef;
@ViewChild('myInputIn') myInputIn: ElementRef;


private element: HTMLInputElement;
private element2: HTMLInputElement;

constructor( private databaseService : DatabaseService,
private router : Router)




ngOnInit()
this.element = this.myInputOut.nativeElement;
this.element2 = this.myInputIn.nativeElement;




For some reason myInputOut returns properly but myInputIn returns undefined.
How can I solve this?










share|improve this question









New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    try to remove comment opening <!-- before <div class="row form-group2 required">

    – Andriy
    Mar 6 at 13:56











  • you are not closing required-input correctly .. try to close it

    – Mohamed Ali RACHID
    Mar 6 at 13:58












  • @Andriy That was the problem, thanks.

    – Zak
    Mar 6 at 14:23











  • Please make sure the code that you post here is well formatted and syntactically right.

    – nircraft
    Mar 6 at 14:24













-2












-2








-2








I'm trying to use @ViewChild to get the DOM element that I need. I have the following component that describes my problem:



import Component, ViewChild, ElementRef, OnInit, Input from "@angular/core";


@Component(
selector: 'some-comp',
template: `

<input
#myInputOut
type="text"
class="google-place-input"
placeholder="Type to search..">

<span class="row form-group">
<required-input
class="col-12 has-danger"
[label]="'somel:'"
[controlName]="'somel'"
[form]="group"
</required-input>
</span>

<div class="row form-group2 required">
<label class=" col-3 control-label" for="street">label:</label>
<input #myInputIn class="col-7" id="someid" placeholder="Type to search.." /></div>
`
)
export class someClass implements OnInit

@ViewChild('myInputOut') myInputOut: ElementRef;
@ViewChild('myInputIn') myInputIn: ElementRef;


private element: HTMLInputElement;
private element2: HTMLInputElement;

constructor( private databaseService : DatabaseService,
private router : Router)




ngOnInit()
this.element = this.myInputOut.nativeElement;
this.element2 = this.myInputIn.nativeElement;




For some reason myInputOut returns properly but myInputIn returns undefined.
How can I solve this?










share|improve this question









New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm trying to use @ViewChild to get the DOM element that I need. I have the following component that describes my problem:



import Component, ViewChild, ElementRef, OnInit, Input from "@angular/core";


@Component(
selector: 'some-comp',
template: `

<input
#myInputOut
type="text"
class="google-place-input"
placeholder="Type to search..">

<span class="row form-group">
<required-input
class="col-12 has-danger"
[label]="'somel:'"
[controlName]="'somel'"
[form]="group"
</required-input>
</span>

<div class="row form-group2 required">
<label class=" col-3 control-label" for="street">label:</label>
<input #myInputIn class="col-7" id="someid" placeholder="Type to search.." /></div>
`
)
export class someClass implements OnInit

@ViewChild('myInputOut') myInputOut: ElementRef;
@ViewChild('myInputIn') myInputIn: ElementRef;


private element: HTMLInputElement;
private element2: HTMLInputElement;

constructor( private databaseService : DatabaseService,
private router : Router)




ngOnInit()
this.element = this.myInputOut.nativeElement;
this.element2 = this.myInputIn.nativeElement;




For some reason myInputOut returns properly but myInputIn returns undefined.
How can I solve this?







angular dom viewchild






share|improve this question









New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 6 at 14:58









nircraft

1,702521




1,702521






New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 6 at 13:53









ZakZak

11




11




New contributor




Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Zak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    try to remove comment opening <!-- before <div class="row form-group2 required">

    – Andriy
    Mar 6 at 13:56











  • you are not closing required-input correctly .. try to close it

    – Mohamed Ali RACHID
    Mar 6 at 13:58












  • @Andriy That was the problem, thanks.

    – Zak
    Mar 6 at 14:23











  • Please make sure the code that you post here is well formatted and syntactically right.

    – nircraft
    Mar 6 at 14:24












  • 1





    try to remove comment opening <!-- before <div class="row form-group2 required">

    – Andriy
    Mar 6 at 13:56











  • you are not closing required-input correctly .. try to close it

    – Mohamed Ali RACHID
    Mar 6 at 13:58












  • @Andriy That was the problem, thanks.

    – Zak
    Mar 6 at 14:23











  • Please make sure the code that you post here is well formatted and syntactically right.

    – nircraft
    Mar 6 at 14:24







1




1





try to remove comment opening <!-- before <div class="row form-group2 required">

– Andriy
Mar 6 at 13:56





try to remove comment opening <!-- before <div class="row form-group2 required">

– Andriy
Mar 6 at 13:56













you are not closing required-input correctly .. try to close it

– Mohamed Ali RACHID
Mar 6 at 13:58






you are not closing required-input correctly .. try to close it

– Mohamed Ali RACHID
Mar 6 at 13:58














@Andriy That was the problem, thanks.

– Zak
Mar 6 at 14:23





@Andriy That was the problem, thanks.

– Zak
Mar 6 at 14:23













Please make sure the code that you post here is well formatted and syntactically right.

– nircraft
Mar 6 at 14:24





Please make sure the code that you post here is well formatted and syntactically right.

– nircraft
Mar 6 at 14:24












3 Answers
3






active

oldest

votes


















0














Looks like you are commenting the input or at least started to



 <!-- <div class="row form-group2 required">
<label class=" col-3 control-label" for="street">label:</label>
<input #myInputIn class="col-7" id="someid" placeholder="Type to search.." />


"<!--" shouldn't be there






share|improve this answer






























    0














    You will be able to access ViewChild queries only inside ngAfterViewInit().
    It's not available in ngOnInit() hence you are getting undefined.






    share|improve this answer























    • > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

      – nircraft
      Mar 6 at 14:15


















    0














    There are some errors in the code, kindly fix it and try again.



    <required-input
    class="col-12 has-danger"
    [label]="'somel:'"
    [controlName]="'somel'"
    [form]="group"


    there should be a closing tag --> [form]="group" >



    <!--<div class="row form-group2 required">


    Remove "<!--" before the <div class="row form-group2 required"> and close it properly with </div>



    Finally, declare input type type="text"



    <input #myInputIn type="text" class="col-7" id="someid" placeholder="Type to search.." />





    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/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
      );



      );






      Zak is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55024754%2fviewchild-returns-undefined-angular-2%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Looks like you are commenting the input or at least started to



       <!-- <div class="row form-group2 required">
      <label class=" col-3 control-label" for="street">label:</label>
      <input #myInputIn class="col-7" id="someid" placeholder="Type to search.." />


      "<!--" shouldn't be there






      share|improve this answer



























        0














        Looks like you are commenting the input or at least started to



         <!-- <div class="row form-group2 required">
        <label class=" col-3 control-label" for="street">label:</label>
        <input #myInputIn class="col-7" id="someid" placeholder="Type to search.." />


        "<!--" shouldn't be there






        share|improve this answer

























          0












          0








          0







          Looks like you are commenting the input or at least started to



           <!-- <div class="row form-group2 required">
          <label class=" col-3 control-label" for="street">label:</label>
          <input #myInputIn class="col-7" id="someid" placeholder="Type to search.." />


          "<!--" shouldn't be there






          share|improve this answer













          Looks like you are commenting the input or at least started to



           <!-- <div class="row form-group2 required">
          <label class=" col-3 control-label" for="street">label:</label>
          <input #myInputIn class="col-7" id="someid" placeholder="Type to search.." />


          "<!--" shouldn't be there







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 6 at 14:01









          joaqcidjoaqcid

          412




          412























              0














              You will be able to access ViewChild queries only inside ngAfterViewInit().
              It's not available in ngOnInit() hence you are getting undefined.






              share|improve this answer























              • > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

                – nircraft
                Mar 6 at 14:15















              0














              You will be able to access ViewChild queries only inside ngAfterViewInit().
              It's not available in ngOnInit() hence you are getting undefined.






              share|improve this answer























              • > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

                – nircraft
                Mar 6 at 14:15













              0












              0








              0







              You will be able to access ViewChild queries only inside ngAfterViewInit().
              It's not available in ngOnInit() hence you are getting undefined.






              share|improve this answer













              You will be able to access ViewChild queries only inside ngAfterViewInit().
              It's not available in ngOnInit() hence you are getting undefined.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 6 at 14:13









              emkayemkay

              189111




              189111












              • > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

                – nircraft
                Mar 6 at 14:15

















              • > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

                – nircraft
                Mar 6 at 14:15
















              > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

              – nircraft
              Mar 6 at 14:15





              > View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild

              – nircraft
              Mar 6 at 14:15











              0














              There are some errors in the code, kindly fix it and try again.



              <required-input
              class="col-12 has-danger"
              [label]="'somel:'"
              [controlName]="'somel'"
              [form]="group"


              there should be a closing tag --> [form]="group" >



              <!--<div class="row form-group2 required">


              Remove "<!--" before the <div class="row form-group2 required"> and close it properly with </div>



              Finally, declare input type type="text"



              <input #myInputIn type="text" class="col-7" id="someid" placeholder="Type to search.." />





              share|improve this answer



























                0














                There are some errors in the code, kindly fix it and try again.



                <required-input
                class="col-12 has-danger"
                [label]="'somel:'"
                [controlName]="'somel'"
                [form]="group"


                there should be a closing tag --> [form]="group" >



                <!--<div class="row form-group2 required">


                Remove "<!--" before the <div class="row form-group2 required"> and close it properly with </div>



                Finally, declare input type type="text"



                <input #myInputIn type="text" class="col-7" id="someid" placeholder="Type to search.." />





                share|improve this answer

























                  0












                  0








                  0







                  There are some errors in the code, kindly fix it and try again.



                  <required-input
                  class="col-12 has-danger"
                  [label]="'somel:'"
                  [controlName]="'somel'"
                  [form]="group"


                  there should be a closing tag --> [form]="group" >



                  <!--<div class="row form-group2 required">


                  Remove "<!--" before the <div class="row form-group2 required"> and close it properly with </div>



                  Finally, declare input type type="text"



                  <input #myInputIn type="text" class="col-7" id="someid" placeholder="Type to search.." />





                  share|improve this answer













                  There are some errors in the code, kindly fix it and try again.



                  <required-input
                  class="col-12 has-danger"
                  [label]="'somel:'"
                  [controlName]="'somel'"
                  [form]="group"


                  there should be a closing tag --> [form]="group" >



                  <!--<div class="row form-group2 required">


                  Remove "<!--" before the <div class="row form-group2 required"> and close it properly with </div>



                  Finally, declare input type type="text"



                  <input #myInputIn type="text" class="col-7" id="someid" placeholder="Type to search.." />






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 6 at 14:20









                  Nishant TamilselvanNishant Tamilselvan

                  326




                  326




















                      Zak is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      Zak is a new contributor. Be nice, and check out our Code of Conduct.












                      Zak is a new contributor. Be nice, and check out our Code of Conduct.











                      Zak is a new contributor. Be nice, and check out our Code of Conduct.














                      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%2f55024754%2fviewchild-returns-undefined-angular-2%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

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко