如何在 Angular2 中使用具有反应形式的“ckeditor”?

use*_*852 4 ckeditor angular2-forms angular-reactive-forms

我是 angular2 的新手。我正在使用 ckeditor 创建一个反应式表单。一切都很好,但我的 ckeditor 数据没有发布。它是空的。我正在阅读指南并创建它,但我无法获得完美的结果。

im user form[formGroup] createPage 并在此表单中使用 ckditor。但我无法获得ckeditor的数据。我如何使用表单从 ckeditor 获取数据。在表单提交按钮中单击。

这是我的 ckeditor.ts 文件

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' );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是 page.ts 文件

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)
       );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是 html 文件

 <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>
Run Code Online (Sandbox Code Playgroud)

use*_*839 6

为什么不简单:

<form [formGroup]="form">
  <ckeditor [config]="config" formControlName="[YOUR_FORMCONTROL_NAME]"></ckeditor>
</form>
Run Code Online (Sandbox Code Playgroud)

为我工作。


rav*_*o10 2

我正在使用 Angular CLI 和通过 NPM 安装的 ckeditor5。

该 CKEditor 位于反应式表单内。

我已经预先制作了输入、NgModel 和设置变量,您在此处看不到。

您可以尝试将其中一些逻辑转换为 Angular 2。

代码:

HTML

//...
<ckeditor [editor]="Editor" [config]="EditorConfig" (change)="change($event)" [(ngModel)]="editorData" [ngModelOptions]="{standalone: true}"></ckeditor>
Run Code Online (Sandbox Code Playgroud)

打字稿

// ...
change({ editor }: ChangeEvent) {
  const EditorData = editor.getData();

  this.form.get('description').setValue(EditorData);
}
Run Code Online (Sandbox Code Playgroud)