Ionic2/Angular2/TypeScript:如何访问函数2016中的DOM元素

Dav*_*ave 8 getelementbyid typescript ionic2 angular

我似乎找不到以正确方式访问DOM元素的"正确"方法:
Angular2 + Ionic2 + TypeScript.

所以鉴于以下情况......

问)如何sketchElement在sign()函数中访问?

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">
Run Code Online (Sandbox Code Playgroud)

我的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.      
  }
}
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 13

你可以使用ViewChild装饰器.首先添加:

<img id="sketchElement"
     #sketchElement
     src="img/logo.png" padding *ngIf="showSignatureView">
Run Code Online (Sandbox Code Playgroud)

并在您的组件中,添加相应的属性:

@Page({
  templateUrl: 'build/pages/settings/settings.html'
})
export class SettingsPage {
  @ViewChild('sketchElement')
  sketchElement:ElementRef;

  ngAfterViewInit() {
    // sketchElement is usable
  }
}
Run Code Online (Sandbox Code Playgroud)

请参阅此plunkr:http://plnkr.co/edit/0TV3ppA0Gpwr5CjOWlAu?p = preview .

这个问题也可以帮到你: