Reputation: 675
New to angularjs 2, I need to use @viewChild but it throws error as 'Cannot read property 'rotateLeft' of undefined'. parent :
<div><pdfReader [imageUrl]="imgsource" *ngIf="holeDocument.ext == 'pdf'"></pdfReader></div>
.ts
import { Component ,ViewChild } from '@angular/core';
import {PdfReader} from "../../shared/pdfreader/pdfReader";
@Component({
selector: 'docView',
templateUrl: 'docView.html',
styleUrls : ["docView.less"]
})
export class DocViewComponent{
@ViewChild('PdfReader') public pdfReader: PdfReader;
constructor(){}
ngAfterViewInit(){
this.imageRotate();
}
imageRotate(){
this.pdfReader.rotateLeft();
}}
Child :
@Component({
selector: 'pdfReader',
templateUrl: 'pdfReader.html'
})
export class PdfReader{
@Input() imageUrl : any;
rotateLeft(){
console.log('rotateLeft called');
this.rotationAngel -= 90;
}
}
Upvotes: 3
Views: 1145
Reputation: 658067
If you use the component type to query, then the quotes are redundant
@ViewChild(PdfReader) public pdfReader: PdfReader;
If you use a string as query there needs to be a matching template variable
<div><pdfReader #PdfReader [imageUrl]="imgsource" *ngIf="holeDocument.ext == 'pdf'"></pdfReader></div>
See also angular 2 / typescript : get hold of an element in the template
Upvotes: 2
Reputation: 148
*ngIf="holeDocument.ext == 'pdf'"
*ngIf is doing the trick. element is not rendered until that condition has met.
use [hidden] instead.
<pdfReader [imageUrl]="imgsource" [hidden]="holeDocument.ext != 'pdf'"></pdfReader>
Upvotes: 1