I have an Angular 1 app that works with a simple contentEditable directive, which can be used like this in templates:
<span contenteditable="true" ng-model="model.property" placeholder="Something">
Editing the element would fire $setViewValue(element.html() and it worked as expected.
I would like to make something in Angular2 with a similarly succinct template syntax. Ideally, I would like the template to look like this:
<span contentEditable="true" [(myProperty)]="name"></span>
where 'name' is a property on the component and have the directive update the component when changed. I feel like I'm close with this (Plunker Link):
//our root app component
import {Component, Input, Output Directive, ElementRef, Renderer, OnInit} from 'angular2/core'
@Directive({
selector: '[contentEditable]',
host: {
'(blur)': 'update($event)'
}
})
export class contentEditableDirective implements OnInit {
@Input() myProperty;
constructor(private el: ElementRef, private renderer: Renderer){}
update(event){
this.myProperty = this.el.nativeElement.innerText;
}
ngOnInit(){
this.el.nativeElement.innerText = this.myProperty;
}
}
This idea works if I pass an object like {name: "someName"} but if just pass a property it seems like it's passing the value, but not the reference and so the binding doesn't flow back to the component. Is there a way to do this that will still allow a template syntax that isn't verbose but still allows easy reuse of the directive.
<myElement contentEditable="true" [myobject]="anobject" property="somevalue">Then I can manipulatemyobject[property]from the directive. Would prefer a cleaner way though. - Mark