Angular 2满足

Cod*_*apu 21 html css3 angular

在Angular 2中,如何使用contenteditable div进行双向数据绑定?

<div class="editable" contenteditable="true">
    <h1>Text Field</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In pharetra felis in sem porta feugiat.</p>
 </div>
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 16

我已经调整了Isetty的答案,以便与Angular 2.0的发布版本一起使用,现在它已经可用了.除了使用发布版本之外,我还添加了一个keyup事件并使用了textContent而不是innerText,因为它更适合我的应用程序.你可能希望改变这些事情.

import {Directive, ElementRef, Input, Output, EventEmitter, OnChanges} from "@angular/core";

@Directive({
    selector: '[contenteditableModel]',
    host: {
        '(blur)': 'onEdit()',
        '(keyup)': 'onEdit()'
    }
})

export class ContentEditableDirective implements OnChanges {
    @Input('contenteditableModel') model: any;
    @Output('contenteditableModelChange') update = new EventEmitter();

    constructor(
        private elementRef: ElementRef
    ) {
        console.log('ContentEditableDirective.constructor');
    }

    ngOnChanges(changes) {
        console.log('ContentEditableDirective.ngOnChanges');
        console.log(changes);
        if (changes.model.isFirstChange())
            this.refreshView();
    }

    onEdit() {
        console.log('ContentEditableDirective.onEdit');
        var value = this.elementRef.nativeElement.innerText
        this.update.emit(value)
    }

    private refreshView() {
        console.log('ContentEditableDirective.refreshView');
        this.elementRef.nativeElement.textContent = this.model
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我试图做一个TSLint默认兼容的版本,但我没有解决方法来修复no-input-rename和no-output-rename.请参阅:https://gist.github.com/zamber/f2d15337c245285d498d9a6b94de3117我还添加了一些不错的东西,比如输入接受,esc取消并发出全值更改,而不是每个keydown. (2认同)

Ise*_*mar 1

请参考这段代码。我想这会对你有用。

应用程序.ts

@Component({
    selector: 'test-component'
})
@View({
    directives: [ContenteditableModel]
    template: `
        <h1 contenteditable="true" [(contenteditableModel)]="someObj.someProperty"></h1>
        {{someObj | json}}
    `
})
export class TestCmp {
    someObj = {someProperty: "startValue"}
}
Run Code Online (Sandbox Code Playgroud)

内容可编辑模型.ts:

import {Directive, ElementRef, Input, Output} from "angular2/core";
import {EventEmitter} from "angular2/src/facade/async";
import {OnChanges} from "angular2/core";
import {isPropertyUpdated} from "angular2/src/common/forms/directives/shared";

@Directive({
    selector: '[contenteditableModel]',
    host: {
        '(blur)': 'onBlur()'
    }
})
export class ContenteditableModel implements OnChanges {
    @Input('contenteditableModel') model: any;
    @Output('contenteditableModelChange') update = new EventEmitter();

    private lastViewModel: any;


    constructor(private elRef: ElementRef) {
    }

    ngOnChanges(changes) {
        if (isPropertyUpdated(changes, this.lastViewModel)) {
            this.lastViewModel = this.model
            this.refreshView()
        }
    }

    onBlur() {
        var value = this.elRef.nativeElement.innerText
        this.lastViewModel = value
        this.update.emit(value)
    }

    private refreshView() {
        this.elRef.nativeElement.innerText = this.model
    }
}
Run Code Online (Sandbox Code Playgroud)

对于额外的输入,我为您找到了一个链接。 https://www.namekdev.net/2016/01/two-way-binding-to-contenteditable-element-in-angular-2/