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:
- 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()
?
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>