@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
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
New contributor
add a comment |
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
New contributor
1
try to remove comment opening<!--
before<div class="row form-group2 required">
– Andriy
Mar 6 at 13:56
you are not closingrequired-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
add a comment |
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
New contributor
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
angular dom viewchild
New contributor
New contributor
edited Mar 6 at 14:58
nircraft
1,702521
1,702521
New contributor
asked Mar 6 at 13:53
ZakZak
11
11
New contributor
New contributor
1
try to remove comment opening<!--
before<div class="row form-group2 required">
– Andriy
Mar 6 at 13:56
you are not closingrequired-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
add a comment |
1
try to remove comment opening<!--
before<div class="row form-group2 required">
– Andriy
Mar 6 at 13:56
you are not closingrequired-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
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
You will be able to access ViewChild queries only inside ngAfterViewInit().
It's not available in ngOnInit() hence you are getting undefined.
> View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild
– nircraft
Mar 6 at 14:15
add a comment |
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.." />
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Mar 6 at 14:01
joaqcidjoaqcid
412
412
add a comment |
add a comment |
You will be able to access ViewChild queries only inside ngAfterViewInit().
It's not available in ngOnInit() hence you are getting undefined.
> View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild
– nircraft
Mar 6 at 14:15
add a comment |
You will be able to access ViewChild queries only inside ngAfterViewInit().
It's not available in ngOnInit() hence you are getting undefined.
> View queries are set before the ngAfterViewInit callback is called. : angular.io/api/core/ViewChild
– nircraft
Mar 6 at 14:15
add a comment |
You will be able to access ViewChild queries only inside ngAfterViewInit().
It's not available in ngOnInit() hence you are getting undefined.
You will be able to access ViewChild queries only inside ngAfterViewInit().
It's not available in ngOnInit() hence you are getting undefined.
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
add a comment |
> 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
add a comment |
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.." />
add a comment |
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.." />
add a comment |
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.." />
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.." />
answered Mar 6 at 14:20
Nishant TamilselvanNishant Tamilselvan
326
326
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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