如何在 Angular 2 中获取`DOM 元素`? [英] How to get `DOM Element` in Angular 2?
问题描述
我有一个具有 元素的组件.它的
(click)
事件会将其更改为 .因此,用户可以编辑数据.我的问题是:
I have a component that has a <p>
element. It's (click)
event will change it into a <textarea>
. So, the user can edit the data. My question is:
- 如何将焦点放在
textarea
上? - 如何获取元素,以便在其上应用
.focus()
? - 我可以避免使用
document.getElementById()
吗?
- How can I make the focus on the
textarea
? - How can I get the element, so I can apply the
.focus()
on it? - Can I avoid using
document.getElemenntById()
?
我尝试使用ElementRef"和@ViewChild()"但是似乎我错过了一些东西:
I have tried to use the "ElementRef" and the "@ViewChild()" however it seems that I'm missing something:
app.component.ts
@ViewChild('tasknoteId') taskNoteRef:ElementRef;
noteEditMode: boolean = false;
get isShowNote (){
return !this.noteEditMode && this.todo.note ? true : false;
}
taskNote: string;
toggleNoteEditMode () {
this.noteEditMode = !this.noteEditMode;
this.renderer.invokeElementMethod(
this.taskNoteRef.nativeElement,'focus'
);
}
app.component.html
<span class="the-insert">
<form [hidden]="!noteEditMode && todo.note">
<textarea #tasknoteId id="tasknote"
name="tasknote"
[(ngModel)]="todo.note"
placeholder="{{ notePlaceholder }}"
style="background-color:pink"
(blur)="updateNote()" (click)="toggleNoteEditMode()"
[autofocus]="noteEditMode"
[innerHTML]="todo.note">
</textarea>
</form>
</span>
推荐答案
使用 ViewChild 和 #localvariable 如下所示,
Use ViewChild with #localvariable as shown here,
<textarea #someVar id="tasknote"
name="tasknote"
[(ngModel)]="taskNote"
placeholder="{{ notePlaceholder }}"
style="background-color: pink"
(blur)="updateNote() ; noteEditMode = false " (click)="noteEditMode = false"> {{ todo.note }}
</textarea>
在组件中,
import {ElementRef} from '@angular/core';
@ViewChild('someVar') el:ElementRef;
ngAfterViewInit()
{
this.el.nativeElement.focus();
}
import {ElementRef} from '@angular/core';
@ViewChild('someVar') el:ElementRef;
constructor(private rd: Renderer) {}
ngAfterViewInit() {
this.rd.invokeElementMethod(this.el.nativeElement,'focus');
}
<小时>更新于 22/03(March)/2017
请注意 Angular v4.0.0-rc.3 (2017-03-10) 中的一些内容已更改.由于 Angular 团队将弃用 invokeElementMethod
,以上代码不能再使用.
Please note from Angular v4.0.0-rc.3 (2017-03-10) few things have been changed.
Since Angular team will deprecate invokeElementMethod
, above code no longer can be used.
重大变化
从 4.0 rc.1 开始:
since 4.0 rc.1:
将 RendererV2 重命名为 Renderer2
将 RendererTypeV2 重命名为 RendererType2
将 RendererFactoryV2 重命名为 RendererFactory2
rename RendererV2 to Renderer2
rename RendererTypeV2 to RendererType2
rename RendererFactoryV2 to RendererFactory2
import {ElementRef,Renderer2} from '@angular/core';
@ViewChild('someVar') el:ElementRef;
constructor(private rd: Renderer2) {}
ngAfterViewInit() {
console.log(this.rd);
this.el.nativeElement.focus(); //<<<=====same as oldest way
}
console.log(this.rd)
会给你以下方法,你现在可以看到 invokeElementMethod
不在这里.附加 img 尚未记录.
注意:你可以使用Rendere2
的以下方法有/没有ViewChild变量来做很多事情.
NOTE: You can use following methods of Rendere2
with/without ViewChild variable to do so many things.
这篇关于如何在 Angular 2 中获取`DOM 元素`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!