I am in new in angular2. I am create a reactive form with ckeditor. all is well but my ckeditor data not post. it's null. i am read guide and create this but i am not able to get perfect result.
i m user form[formGroup] createPage and use ckditor in this form. but i can't get data of ckeditor. how can i get data from ckeditor using form. in form submit button click.
this is my ckeditor.ts file
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'CKEDITOR',
template: `
<textarea name="targetId" id="targetId" rows="rows" cols="cols">
{{content}}
</textarea>`
})
export class AdminCkeditorComponent implements OnInit {
content: any = "test content";
@Input() targetId;;
@Input() rows = 10;
@Input() cols;
constructor() { }
ngOnInit(){
window['CKEDITOR']['replace']( 'targetId' );
}
}
this is page.ts file
import { FormGroup, FormControl } from '@angular/forms';
import { Component, OnInit, Directive, ViewChild, ElementRef } from '@angular/core';
import {Http, Response, Headers, RequestOptions} from "@angular/http";
import {Observable} from "rxjs/Rx";
import 'rxjs/add/operator/map'
@Component({
selector: 'app-create-page',
templateUrl: './create-page.html'
})
export class CreatePageComponent implements OnInit {
constructor (private http: Http) {}
ngOnInit() {}
createPage = new FormGroup({
pageTitle: new FormControl(),
pageUrl: new FormControl(),
pageHeader: new FormControl(),
pageBody: new FormControl()
})
public result : String;
onSubmitPage(){
console.log(this.createPage.value.pageTitle);
console.log(this.createPage.value.pageUrl);
console.log(this.createPage.value.pageHeader);
console.log(this.createPage.value.pageBody);
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-from-urlencoded');
headers.append('Access-Control-Allow-Origin', '*');
let options = new RequestOptions();
let body =this.createPage.value;
this.http.post('http://192.168.0.130:8012/api/createPage', body, options)
.map(res => res.json())
.subscribe(
data => this.result = data,
err => console.log('ERROR!!!'+err),
() => console.log('Got response from API', this.result)
);
}
}
this is html file
<form [formGroup]="createPage" (ngSubmit)="onSubmitPage()">
<input type="text" name="pageTitle" formControlName="pageTitle" class="form-control">
<input type="text" name="pageUrl" formControlName="pageUrl" class="form-control" >
<textarea name="pageHeader" formControlName="pageHeader" class="form-control"></textarea>
<CKEDITOR></CKEDITOR>
<button class="btn btn-info pull-right">Sign in</button>
</form>
Why not simply:
<form [formGroup]="form">
<ckeditor [config]="config" formControlName="[YOUR_FORMCONTROL_NAME]"></ckeditor>
</form>
Works for me.
I am using Angular CLI with ckeditor5 installed with NPM.
This CKEditor is inside a reactive form.
I have pre-made the Input, the NgModel and the setting variable which you don't see here.
You can try and translate some of this logic to Angular 2.
HTML:
//...
<ckeditor [editor]="Editor" [config]="EditorConfig" (change)="change($event)" [(ngModel)]="editorData" [ngModelOptions]="{standalone: true}"></ckeditor>
TypeScript:
// ...
change({ editor }: ChangeEvent) {
const EditorData = editor.getData();
this.form.get('description').setValue(EditorData);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With