如何在 angular2 的 div 的 contenteditable 上使用 [(ngModel)]?

How to use [(ngModel)] on div's contenteditable in angular2?

我正在尝试使用 ngModel 以两种方式绑定 div 的 contenteditable 输入内容,如下所示:

<div id="replyiput" class="btn-input"  [(ngModel)]="replyContent"  contenteditable="true" data-text="type..." style="outline: none;"    ></div> 

但它不工作并发生错误:

EXCEPTION: No value accessor for '' in [ddd in PostContent@64:141]
app.bundle.js:33898 ORIGINAL EXCEPTION: No value accessor for ''

NgModel 期望绑定元素具有 value 属性,而 div 没有。这就是您收到 No value accessor 错误的原因。

您可以使用 textContent 属性(而不是 value)和 input 事件来设置自己的等效 属性 和事件数据绑定:

import {Component} from 'angular2/core';
@Component({
  selector: 'my-app',
  template: `{{title}}
    <div contenteditable="true" 
     [textContent]="model" (input)="model=$event.target.textContent"></div>
    <p>{{model}}`
})
export class AppComponent {
  title = 'Angular 2 RC.4';
  model = 'some text';
  constructor() { console.clear(); }
}

Plunker

我不知道 contenteditable 的所有浏览器是否都支持 input 事件。您始终可以绑定到某个键盘事件。

此处使用 Plunkr http://plnkr.co/edit/j9fDFc,但相关代码在下方。


绑定并手动更新 textContent 对我不起作用,它不处理换行符(在 Chrome 中,在换行符后键入会使光标跳回到开头)但是我能够使用来自 https://www.namekdev.net/2016/01/two-way-binding-to-contenteditable-element-in-angular-2/.

的 contenteditable 模型指令让它工作

我使用 white-space: pre-wrap 对其进行了调整以处理多行纯文本(使用 \ns,而不是 <br>s),并将其更新为使用 keyup而不是 blur。请注意,此问题的某些解决方案使用 input 事件,IE 或 Edge 的 contenteditable 元素尚不支持该事件。

代码如下:

指令:

import {Directive, ElementRef, Input, Output, EventEmitter, SimpleChanges} from 'angular2/core';

@Directive({
  selector: '[contenteditableModel]',
  host: {
    '(keyup)': 'onKeyup()'
  }
})
export class ContenteditableModel {
  @Input('contenteditableModel') model: string;
  @Output('contenteditableModelChange') update = new EventEmitter();

  /**
   * By updating this property on keyup, and checking against it during
   * ngOnChanges, we can rule out change events fired by our own onKeyup.
   * Ideally we would not have to check against the whole string on every
   * change, could possibly store a flag during onKeyup and test against that
   * flag in ngOnChanges, but implementation details of Angular change detection
   * cycle might make this not work in some edge cases?
   */
  private lastViewModel: string;

  constructor(private elRef: ElementRef) {
  }

  ngOnChanges(changes: SimpleChanges) {
    if (changes['model'] && changes['model'].currentValue !== this.lastViewModel) {
      this.lastViewModel = this.model;
      this.refreshView();
    }
  }

  /** This should probably be debounced. */
  onKeyup() {
    var value = this.elRef.nativeElement.innerText;
    this.lastViewModel = value;
    this.update.emit(value);
  }

  private refreshView() {
    this.elRef.nativeElement.innerText = this.model
  }
}

用法:

import {Component} from 'angular2/core'
import {ContenteditableModel} from './contenteditable-model'

@Component({
  selector: 'my-app',
  providers: [],
  directives: [ContenteditableModel],
  styles: [
    `div {
      white-space: pre-wrap;

      /* just for looks: */
      border: 1px solid coral;
      width: 200px;
      min-height: 100px;
      margin-bottom: 20px;
    }`
  ],
  template: `
    <b>contenteditable:</b>
    <div contenteditable="true" [(contenteditableModel)]="text"></div>

    <b>Output:</b>
    <div>{{text}}</div>

    <b>Input:</b><br>
    <button (click)="text='Success!'">Set model to "Success!"</button>
  `
})
export class App {
  text: string;

  constructor() {
    this.text = "This works\nwith multiple\n\nlines"
  }
}

目前仅在 Chrome 和 Linux 的 FF 中测试过。

更新答案 (2017-10-09):

现在我有了 ng-contenteditable 模块。它与 Angular 形式的兼容性。

旧答案(2017-05-11): 就我而言,我可以简单地做到:

<div
  contenteditable="true"
  (input)="post.postTitle = $event.target.innerText"
  >{{ postTitle }}</div>

其中 post - 它是 属性 postTitle 的对象。

第一次,在 ngOnInit() 并从后端获取 post 之后,我在我的组件中设置了 this.postTitle = post.postTitle

这里是another version,基于@tobek的回答,同样支持html和粘贴:

import {
  Directive, ElementRef, Input, Output, EventEmitter, SimpleChanges, OnChanges,
  HostListener, Sanitizer, SecurityContext
} from '@angular/core';

@Directive({
  selector: '[contenteditableModel]'
})
export class ContenteditableDirective implements OnChanges {
  /** Model */
  @Input() contenteditableModel: string;
  @Output() contenteditableModelChange?= new EventEmitter();
  /** Allow (sanitized) html */
  @Input() contenteditableHtml?: boolean = false;

  constructor(
    private elRef: ElementRef,
    private sanitizer: Sanitizer
  ) { }

  ngOnChanges(changes: SimpleChanges) {
    if (changes['contenteditableModel']) {
      // On init: if contenteditableModel is empty, read from DOM in case the element has content
      if (changes['contenteditableModel'].isFirstChange() && !this.contenteditableModel) {
        this.onInput(true);
      }
      this.refreshView();
    }
  }

  @HostListener('input') // input event would be sufficient, but isn't supported by IE
  @HostListener('blur')  // additional fallback
  @HostListener('keyup') onInput(trim = false) {
    let value = this.elRef.nativeElement[this.getProperty()];
    if (trim) {
      value = value.replace(/^[\n\s]+/, '');
      value = value.replace(/[\n\s]+$/, '');
    }
    this.contenteditableModelChange.emit(value);
  }

  @HostListener('paste') onPaste() {
    this.onInput();
    if (!this.contenteditableHtml) {
      // For text-only contenteditable, remove pasted HTML.
      // 1 tick wait is required for DOM update
      setTimeout(() => {
        if (this.elRef.nativeElement.innerHTML !== this.elRef.nativeElement.innerText) {
          this.elRef.nativeElement.innerHTML = this.elRef.nativeElement.innerText;
        }
      });
    }
  }

  private refreshView() {
    const newContent = this.sanitize(this.contenteditableModel);
    // Only refresh if content changed to avoid cursor loss
    // (as ngOnChanges can be triggered an additional time by onInput())
    if (newContent !== this.elRef.nativeElement[this.getProperty()]) {
      this.elRef.nativeElement[this.getProperty()] = newContent;
    }
  }

  private getProperty(): string {
    return this.contenteditableHtml ? 'innerHTML' : 'innerText';
  }

  private sanitize(content: string): string {
    return this.contenteditableHtml ? this.sanitizer.sanitize(SecurityContext.HTML, content) : content;
  }
}

我已经摆弄过这个解决方案,现在将在我的项目中使用以下解决方案:

<div #topicTitle contenteditable="true" [textContent]="model" (input)="model=topicTitle.innerText"></div>

比起“$event”,我更喜欢使用模板引用变量。

相关link: https://angular.io/guide/user-input#get-user-input-from-a-template-reference-variable

如果您要绑定的是字符串,则这是一个简单的解决方案,不需要任何事件。只需在 table 单元格内放置一个文本框输入并绑定到它。然后将文本框格式设置为透明

HTML:

<tr *ngFor="let x of tableList">
    <td>
        <input type="text" [(ngModel)]="x.value" [ngModelOptions]="{standalone: true}">
    </td>
</tr>

对我来说,使用 javascript 就足够了,无需 ts 对象。 HTML:

 <div
        id="custom-input"
        placeholder="Schreiben..."
</div>

TS:

  • 获取输入值:document.getElementById("custom-input").innerHTML

  • 设置输入值:document.getElementById("custom-input").innerHTML = "myValue"

一切都很完美。我被迫使用 div 而不是 ionic ion-textarea,因为我在自动调整大小方面遇到了问题。使用 ion-textarea 我只能使用 js 进行自动调整。现在我使用 CSS 自动调整大小,我认为这样更好。

contenteditable 中,我借助 blur 实现了 双向绑定 ] 事件和 innerHTML 属性。

在.html:

<div placeholder="Write your message.."(blur)="getContent($event.target.innerHTML)" contenteditable [innerHTML]="content"></div>

在 .ts 中:

getContent(innerText){
  this.content = innerText;
}