1

I am beginner angular 2

Here is my input

<input type="hidden" value={{date}} #myname/>

Here is my code

console.log(document.getElementById('myname')); It's return null

why?

Kindly Advise me,

Thanks

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
Anivaishu
  • 243
  • 1
  • 3
  • 9
  • if you are using angular-cli. you can add jquery to you config file and use it instead of document. like $('#myname') – Yoav Schniederman Mar 18 '17 at 09:39
  • any other way to fix this issue... : ( – Anivaishu Mar 18 '17 at 09:50
  • Possible duplicate of [angular 2 / typescript : get hold of an element in the template](http://stackoverflow.com/questions/32693061/angular-2-typescript-get-hold-of-an-element-in-the-template) – AT82 Mar 18 '17 at 19:18

2 Answers2

1

As you defined template variable, you can access that element using ViewChild decorator. document.getElementById will only work when element has id property with specific value in that id attribute.

@ViewChild('myname') myNameElem: any;
eko
  • 39,722
  • 10
  • 72
  • 98
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Thanks for ur reply.I used this but my consoled value is undefined.Actually here is my code. I defined @ViewChild('myname') myNameElem: any; and console.log(this.myNameElem); – Anivaishu Mar 18 '17 at 07:46
  • I need get input element value.I tried to many way.But Does not wrk. – Anivaishu Mar 18 '17 at 07:50
  • pls refer this link http://stackoverflow.com/questions/42871031/get-element-value-ng-for – Anivaishu Mar 18 '17 at 07:52
1
import {ElementRef} from '@angular/core';

@ViewChild('myname') el:ElementRef;

ngAfterViewInit()
{
   //this.el.nativeElement.focus(); 
   //this.el.nativeElement.value()
   // And more

}
Yoav Schniederman
  • 5,253
  • 3
  • 28
  • 32