Angular 2与contentEditable div和ngModel支持形成

前端之家收集整理的这篇文章主要介绍了Angular 2与contentEditable div和ngModel支持形成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用Angular 2 Forms,但我不想使用输入,而是想使用可编辑的div.

这有效:

  1. <input type="text" [(ngModel)]="value">

但这会引发错误

  1. <div contentEditable="true" [(ngModel)]="value"></div>

TypeError: setting a property that has only a getter

有没有办法在Angular Forms中使用div?我不想使用输入或textarea

不是开箱即用..

ngModel只能在支持它的元素上访问!

您可以创建指令或自定义输入组件.

无论如何,它必须实现此接口ControlValueAccessor.

指示:

工作演示:https://plnkr.co/edit/12vAEFf2OBS3ERu9fhwk?p=preview

  1. import {Directive,Component,NgModule,forwardRef,ViewChild,ElementRef,HostListener,Renderer} from '@angular/core'
  2. import {BrowserModule} from '@angular/platform-browser'
  3. import { FormsModule,ControlValueAccessor,NG_VALUE_ACCESSOR } from '@angular/forms';
  4.  
  5. @Directive({
  6. selector: 'div[contentEditable]',providers: [
  7. {
  8. provide: NG_VALUE_ACCESSOR,useExisting: forwardRef(() => EditableDivDirective),multi: true
  9. }
  10. ]
  11. })
  12. export class EditableDivDirective implements ControlValueAccessor {
  13.  
  14. constructor(private _elRef: ElementRef,private _renderer: Renderer) { }
  15.  
  16. onChange() {
  17. if (this._onChange) {
  18. this._onChange(this._elRef.nativeElement.innerText);
  19. }
  20. }
  21.  
  22. @HostListener('keyup',['$event'])
  23. keyup(event: any) {
  24. this.onChange();
  25. }
  26.  
  27.  
  28. // ControlValueAccessor implementation
  29. // ====================================
  30.  
  31. private _onChange = (_) => { }; // call it if your value changed..
  32. private _onTouched = () => { }; // call it "on blur" ..
  33.  
  34. // will be called if a values comes in via ngModule !
  35. writeValue(val: any) {
  36. if (!val) val = '';
  37.  
  38. this._renderer.setElementProperty(this._elRef.nativeElement,'innerText',val);
  39. }
  40.  
  41. registerOnChange(fn: (_: any) => void): void { this._onChange = fn; }
  42. registerOnTouched(fn: () => void): void { this._onTouched = fn; }
  43. }
  44.  
  45. @Component({
  46. selector: 'my-app',template: `
  47. <div>
  48. <h2>Hello {{name}}</h2>
  49. <div contentEditable="true" [(ngModel)]="name">test test test</div>
  50. </div>
  51. `,})
  52. export class App {
  53. name:string;
  54. constructor() {
  55. this.name = 'Angular2'
  56. }
  57. }
  58.  
  59. @NgModule({
  60. imports: [ BrowserModule,FormsModule ],declarations: [ App,EditableDivDirective ],bootstrap: [ App ]
  61. })
  62. export class AppModule {}

零件:

查看工作演示:https://plnkr.co/edit/XMSTrWSe3gN9iwVTBukz?p=preview

  1. import {Component,ElementRef} from '@angular/core'
  2. import {BrowserModule} from '@angular/platform-browser'
  3. import { FormsModule,NG_VALUE_ACCESSOR } from '@angular/forms';
  4.  
  5. @Component({
  6. selector: 'my-name-input',template: `
  7. <div>
  8. <label>first name</label>
  9. <div #firstName contentEditable="true" (keyup)="onChange()">{{this._firstName}}</div>
  10. <br />
  11. <label>last name</label><input [(ngModel)]="_lastName" (ngModelChange)="onChange()" />
  12. </div>
  13. `,providers: [ // IMPORTANT !!
  14. {
  15. provide: NG_VALUE_ACCESSOR,useExisting: forwardRef(() => MyCompleteNameInputComponent),multi: true
  16. }
  17. ]
  18. })
  19. export class MyCompleteNameInputComponent implements ControlValueAccessor {
  20.  
  21. @ViewChild('firstName') editableDiv: ElementRef;
  22.  
  23. private _firstName: string = '';
  24. private _lastName: string = '';
  25.  
  26. onChange() {
  27.  
  28. this._firstName = this.editableDiv.nativeElement.innerText;
  29.  
  30. if (this._onChange) {
  31. this._onChange(this._firstName + ' ' + this._lastName);
  32. }
  33. }
  34.  
  35.  
  36. // ControlValueAccessor implementation
  37. // ====================================
  38.  
  39. private _onChange = (_) => { }; // call it if your value changed..
  40. private _onTouched = () => { }; // call it "on blur" ..
  41.  
  42. // will be called if a values comes in via ngModule !
  43. writeValue(val: any) {
  44. if (!val || !val.split) val = '';
  45.  
  46. let splitted = val.split(' ');
  47. this._firstName = splitted[0] || '';
  48. this._lastName = splitted[1] || '';
  49. }
  50.  
  51. registerOnChange(fn: (_: any) => void): void { this._onChange = fn; }
  52. registerOnTouched(fn: () => void): void { this._onTouched = fn; }
  53. }
  54.  
  55. @Component({
  56. selector: 'my-app',template: `
  57. <div>
  58. <h2>Hello {{name}}</h2>
  59. <my-name-input [(ngModel)]="name"></my-name-input>
  60. </div>
  61. `,MyCompleteNameInputComponent ],bootstrap: [ App ]
  62. })
  63. export class AppModule {}

猜你在找的Angularjs相关文章