Note: since the problem is a little complex, the code is abstracted for readability
We've a <parent-component>
like this:
<child-component></child-component>
<button (click)="doSomeClick()"> Do Some Click </button>
The template
of the <child-component>
is:
<textarea #childComponentElement #someField="ngModel" name="someName" [(ngModel)]="someModel"></textarea>
We're trying to access the value of this element inside the parent-component.component.ts
like this:
export class ParentComponent implements AfterViewInit {
@ViewChild('childComponentElement') el:ElementRef;
ngAfterViewInit() {
console.log(this.el.nativeElement.value);
}
doSomeClick(){
}
}
However it throws this error:
Cannot read property 'nativeElement' of undefined
What have we tried so far:
- This gives access to
<parent-component>
, we need<textarea>
of<child-component>
- It's not about
angular-tree-component
- The directive name is camelCased
ElementRef
seems to be an old thing- This throws Cannot read property 'nativeElement' of undefined
- How is the reference between
this.element.nativeElement
&<input>
element is getting established? - There is no *ngIf or *ngSwitchCase
- There is no
*ngIf
used with#childComponentElement
- The call is inside
ngAfterViewInit
only - Time out is a very dirty approach