将contenteditable元素切换为true,使用按钮调用编辑元素文本,并在完成编辑时将模糊切换contenteditable元素设置为false!
如何在课堂上将contenteditable设置为false!
import {Component} from 'angular2/core'
@Component({
selector: 'my-app',
template: `
<h2 #el contenteditable="false" (blur)="text=el.textContent">
This Content is Editable
</h2>
<button (click)="toggleContenteditable()">Edit Text Content</button>
<hr/>
<p> Text Obj: {{text}}</p>
`
})
export class App {
text : string;
contenteditable:bool = false;
toggleContenteditable(){
this.contenteditable = !this.contenteditable;
}
}
答案 0 :(得分:9)
您应该在attr.
之前使用contenteditable
前缀的属性绑定,然后在其中传递contenteditable
变量。这有助于您通过contenteditable
方法对toggleContenteditable
执行切换效果。
<h2 #el [attr.contenteditable]="contenteditable" (blur)="text=el.textContent">
同时将toggleContenteditable
功能更改为以下。
toggleContenteditable(){
this.contenteditable = !this.contenteditable; //`this.` was missing in later assignment
}