我似乎无法找到以正确的方式访问DOM元素的“正确”方法:
Angular2 Ionic2 TypeScript.
Angular2 Ionic2 TypeScript.
所以鉴于以下情况……
问)如何在sign()函数中访问sketchElement?
Settings.ts模板:
<button full class="top-button-margin" (click)="sign()"> Sign <ion-icon name="create"></ion-icon> </button> <img id="sketchElement" src="img/logo.png" padding *ngIf="showSignatureView">
我的settings.ts类:
@Page({ templateUrl: 'build/pages/settings/settings.html' }) export class SettingsPage { private currentUser: User = <User>{}; private loggingOut: boolean = false; private currentConnection: string = ""; private showSignatureView: boolean = false; constructor( private _platform: Platform,private _nav: NavController,private _events: Events,private _LoginService: LoginService,private _SessionService: SessionService,private _UIService: UIService) { } sign() { // I want to access sketchElement and its properties here. } }
您可以使用ViewChild装饰器.首先添加:
<img id="sketchElement" #sketchElement src="img/logo.png" padding *ngIf="showSignatureView">
@Page({ templateUrl: 'build/pages/settings/settings.html' }) export class SettingsPage { @ViewChild('sketchElement') sketchElement:ElementRef; ngAfterViewInit() { // sketchElement is usable } }
看到这个plunkr:http://plnkr.co/edit/0TV3ppA0Gpwr5CjOWlAu?p=preview.
这个问题也可以帮到你: