I am trying to make a component that shows data in rows from TableData model,
export class TableData{
constructor(
public id: number,
public
I was looking an answer to the same question. First, I got here. Later, I found amazing article with answer you was looking for.
Updated: I have udpated plunk as well as directive code to latest verstion of angular
Directive: contenteditableModel
https://www.namekdev.net/2016/01/two-way-binding-to-contenteditable-element-in-angular-2/
https://plnkr.co/edit/SRLYoX5chNYJIpZ4iqsG?p=preview
import {
Directive,
ElementRef,
Input,
Output,
OnChanges,
EventEmitter,
HostListener,
SimpleChanges
} from '@angular/core';
/**
*
*/
@Directive({
selector: '[contenteditableModel]'
})
export class ContenteditableModelDirective implements OnChanges {
@Input('contenteditableModel')
public model: any;
@Output('contenteditableModelChange')
public update = new EventEmitter();
private _lastViewModel: any;
constructor(private elRef: ElementRef) {}
public ngOnChanges(changes: SimpleChanges): void {
if(this._lastViewModel !== changes['model'].currentValue){
this._lastViewModel = this.model;
this._refreshView();
}
}
@HostListener('blur')
public onBlur() {
var value = this.elRef.nativeElement.innerText;
this._lastViewModel = value;
this.update.emit(value);
}
private _refreshView() {
this.elRef.nativeElement.innerText = this.model;
}
}