Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular and contenteditable

I've searched the web but can't find a way to work with contenteditable events on Angular 6/7. Angular seems to have a messy solution with it but said feature doesn't seem to be carried over to recent versions.

A use case would be is on a content editable onChange event, call a function:

<div contententeditable="true" [change]="onNameChange(if.there.is.such.a.thing)">Type your name</div>

...

private name: string;

onNameChange(name) {
   this.name = name;
}

Any ideas on this? Thanks.

like image 383
Jhourlad Estrella Avatar asked Nov 03 '18 10:11

Jhourlad Estrella


People also ask

Is Contenteditable angular?

Angular does not have an accessor for contenteditable , so if you want to use it with forms you will have to write one.

What is Contenteditable used for?

The contenteditable attribute specifies whether the content of an element is editable or not.

What is Contenteditable true?

The contenteditable is used to specify whether the element's content is editable by the user or not. Syntax: <element contenteditable="true|false"> This attribute has two values. true: If the value of the contenteditable attribute is set to true then the element is editable.

How do I make Contenteditable in HTML?

You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.


1 Answers

You can use the input event, like so:

<div contenteditable (input)="onNameChange($event.target.innerHTML)">
   Type your name
</div>

Here is a Stackblitz demo

like image 69
user184994 Avatar answered Oct 21 '22 02:10

user184994