Hi I'm trying to implement tinymce into an angular 2 component for a small forum to create a new thread. I want the content of the textarea (tinymce) be 2-way-binded to a variable inside the component. So far the submit button works but the keyup event doesn't.
export class ForumNewThreadComponent implements OnInit{
constructor(){}
ngOnInit():any {
tinymce.init(
{
selector: ".tinyMCE",
})
}
text:string;
submit(){
this.text = tinymce.activeEditor.getContent();
}
getTinymceContent(){
this.text = tinymce.activeEditor.getContent();
}
}
and view
<div class="thread-body">
{{getValue}}
<textarea class="tinyMCE" style="height:300px" (keyup)="getTinymceContent()">
</textarea>
<button class="btn btn-primary" (click)="submit()">Submit</button>
</div>
I wanted to say I did the same implementation as stated above, but I came across this weird error and banged my head around and around fixing this error of
'cannot modify NodeName of Null'
, so finally today I fixed the error and I wanted to share it, so people won't have to search it anymore what the error might be. I know this is an old post my apologies for that.https://github.com/Abhijith-Nagaraja/tinymce-docs/blob/master/integrations/angular2.md#sample-directive-implementation-with-ngmodel-two-way-binding
2. add two variables to the directive
rewrite the method
to
replace in
ValueOnChange(change: boolean)
this.val = tinymce.activeEditor.getContent();
to
rewrite
tinymce.init(options)
to
and last add a
ngOnDestroy
methodThis has fixed the error for me and also fixed for me when the editor was already initialized and I had reuse it, it wouldn't compile. but now because of the
ngOnDestroy
I now can destroy the editor and theafterViewInit
will recall thetinymce.init
Or do it like this, using tmce's change event and NgZone
This would fail once you have more than one instance on tmce in one component. Put this logic in a directive like Thierry's implementation.
I know this is a little old post. But after scratching my head for over 2 days. I was finally able to figure this out and thought this might be useful to others. So sharing it here
https://github.com/Abhijith-Nagaraja/tinymce-docs/blob/master/integrations/angular2.md#sample-directive-implementation-with-ngmodel-two-way-binding
I would implement a custom value accessor for this:
I would use it this way:
and in your component class: