在Angular 2

时间:2017-04-13 20:45:13

标签: angular tinymce

我是Agular 2中的新手,我正在尝试在我的项目中使用TinyMCE编辑器。

我按照这些说明创建和使用tinyMCE组件:https://www.tinymce.com/docs/integrations/angular2/

按照以下步骤操作后,我无法使TinyMCE正常工作,我收到以下错误:

  

错误错误:未捕获(在承诺中):错误:模板解析错误:   无法绑定到'elementId',因为它不是'simple-tiny'的已知属性。

     
      
  1. 如果'simple-tiny'是一个Angular组件并且它有'elementId'输入,那么请验证它是否是该模块的一部分。
  2.   
  3. 如果'simple-tiny'是Web组件,则将'CUSTOM_ELEMENTS_SCHEMA'添加到此组件的'@ NgModule.schemas'以禁止显示此消息。
  4.   
  5. 要允许任何属性将“NO_ERRORS_SCHEMA”添加到此组件的“@ NgModule.schemas”。 (“
  6.   

这是我的代码:

微小-editor.component.ts

import { Component, OnChanges, AfterViewInit, EventEmitter, OnDestroy, 
Input, Output } from '@angular/core';
import 'tinymce';
import 'tinymce/themes/modern';

declare var tinymce: any;

@Component({
  selector: 'simple-tiny',
  template: `<textarea id="{{elementId}}"></textarea>`
})
export class TinyEditorComponent implements AfterViewInit, OnDestroy {
  @Input() elementId: string;
  @Output() onEditorContentChange = new EventEmitter<any>();

  editor;

  constructor() { }

  ngAfterViewInit() {
    tinymce.init({
      selector: `#${this.elementId}`,
      skin_url: '../assets/skins/lightgray',
      plugins: ['link', 'paste', 'table'],
      setup: editor => {
        this.editor = editor;
        editor.on('keyup change', () => {
          const content = editor.getContent();
          this.onEditorContentChange.emit(content);
        });
      }
    });
  }

  ngOnDestroy() {
    tinymce.remove(this.editor);
  }

}

parent.component.html

<simple-tiny [elementId]="'descripcion'" (onEditorContentChange)="keyupHandler($event)"></simple-tiny>

我已将组件导入 app.module.ts 我已经将脚本添加到 angular-cli.json

我正在使用TinyMCE版本4.5.6

我错过了什么?

这是我的app.module.ts。我正在使用模板。 的 app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';

import { AppComponent } from './app.component';
import { BsDropdownModule } from 'ng2-bootstrap/dropdown';
import { TabsModule } from 'ng2-bootstrap/tabs';
import { NAV_DROPDOWN_DIRECTIVES } from './shared/nav-dropdown.directive';

import { ChartsModule } from 'ng2-charts/ng2-charts';
import { SIDEBAR_TOGGLE_DIRECTIVES } from './shared/sidebar.directive';
import { AsideToggleDirective } from './shared/aside.directive';
import { BreadcrumbsComponent } from './shared/breadcrumb.component';

// Routing Module
import { AppRoutingModule } from './app.routing';

//Layouts
import { FullLayoutComponent } from './layouts/full-layout.component';
import { SimpleLayoutComponent } from './layouts/simple-layout.component';

//Modules
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule, RequestOptions } from '@angular/http';
import { AuthModule } from './auth/auth.module';

//Services
import { AuthGuard } from './auth/auth.guard';
import { AuthService } from './auth/auth.service';
import { PropertiesService } from './services/properties.service';
import { TypesService } from './services/types.service';

import { TinyEditorComponent } from './tiny-editor/tiny-editor.component';

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    BsDropdownModule.forRoot(),
    TabsModule.forRoot(),
    ChartsModule,
    AuthModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule
  ],
  declarations: [
    AppComponent,
    FullLayoutComponent,
    SimpleLayoutComponent,
    NAV_DROPDOWN_DIRECTIVES,
    BreadcrumbsComponent,
    SIDEBAR_TOGGLE_DIRECTIVES,
    AsideToggleDirective,
    TinyEditorComponent
  ],
  providers: [
    {
    provide: LocationStrategy,
    useClass: HashLocationStrategy
    },
    AuthService,
    AuthGuard,
    PropertiesService,
    TypesService
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

不确定构建器问题,可能是angular-cli吗?

1 个答案:

答案 0 :(得分:0)

elementId引用要包装到编辑器的HTML元素,并在指令中作为@Input参数接收。尝试使用:

selector: '#' + this.elementId,

这将是

  ngAfterViewInit() {
    tinymce.init({
      selector: '#' + this.elementId,
      skin_url: '../assets/skins/lightgray',
      plugins: ['link', 'paste', 'table'],
      setup: editor => {
        this.editor = editor;
        editor.on('keyup change', () => {
          const content = editor.getContent();
          this.onEditorContentChange.emit(content);
        });
      }
    });
  }

如果仍然无效,请尝试使用默认选择标识符,即

select: '#wysiwygEditor',

并在HTML中

<simple-tiny id="wysiwygEditor" (onEditorContentChange)="keyupHandler($event)"></simple-tiny>

希望这会有所帮助。欢呼声。