我遇到動態添加的CKEditors問題,通過NgFor
與[email protected]
。Angular2和動態添加的CKEditors中斷
@Directive({
selector: 'textarea'
})
class CKEditor {
constructor(_elm: ElementRef) {
CKEDITOR.replace(_elm.nativeElement);
}
}
@Component({
selector: 'my-app',
})
@View({
directives: [NgFor, CKEditor],
template: `
<div *ng-for="#item of items">
{{ item }}: <textarea>{{ item }}</textarea>
</div>
<button (click)="addItem()">Add</button>`
})
class AppComponent {
items = ['default_0', 'default_1'];
constructor() {
this.addItem();
}
addItem() {
var id = 'ckeditor_inst_' + this.items.length;
this.items.push(id);
}
}
你可以看到,正常工作3個CKEditors。然後點擊底部的「添加」按鈕,打開容器中的最後一個CKEditor,甚至可以寫入它,如果按下任何工具欄按鈕,它會拋出:
Uncaught TypeError: Cannot read property 'getSelection' of undefined
。
有趣的是,只有最後一個CKEditor被破壞,另外兩個工作。看起來Angular2不知何故地操縱了打破CKEditor的最後一個元素。
我記得使用在[email protected]
中添加新的CKEditors的相同方式,我認爲它在那裏工作,但也許我只是沒有注意到。版本[email protected]
is the same。