gpt4 book ai didi

typescript - Ionic2/Angular2/ typescript : How to access a DOM element in a function 2016

转载 作者:太空狗 更新时间:2023-10-29 17:13:10 25 4
gpt4 key购买 nike

我似乎找不到以最新方式访问 DOM 元素的“正确”方法:
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
}
}

查看此插件:http://plnkr.co/edit/0TV3ppA0Gpwr5CjOWlAu?p=preview .

这个问题也可以帮助你:

关于typescript - Ionic2/Angular2/ typescript : How to access a DOM element in a function 2016,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com