【发布时间】:2018-08-06 00:12:42
【问题描述】:
所以我正在创建一个包含内容可编辑单元格的表格,我需要同时更新视觉和打字稿代码,不幸的是我发现的所有替代方案都不起作用,这是一个具有相同问题的 plunker:
https://plnkr.co/edit/PCXNFSUqiHrYedx4E4mW?p=preview
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div contenteditable='true'
(input)='name=$event.target.innerText'
[(innerText)]='name'>
name
</div>
<br>
<div contenteditable='true'
(input)='name=$event.target.innerHtml'
[(innerHtml)]='name'>
name
</div>
<br>
<div contenteditable='true'
(input)='name=$event.target.textContent'
[(textContent)]='name'>
name
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
它适用于 google chrome 和 opera,safari 和 mozilla 使用 textContent 和 innerText 将文本向后写入,并且使用 innerHtml 将值变为未定义。
@更新
对于我的问题,我最终在单元格内使用输入字段
<td [class.active]="selectedCell.row === rowIndex && selectedCell.colunm === 3"
(click)="setSelectedCell(rowIndex, 3, true)"
class="lassoer-info">
<input [id]="'entity' + rowIndex"
(keydown)="onkeyDown($event)"
[(ngModel)]="rowData.entity"
[disabled]="rowData.inscriptionId || !canEdit"
type="text"
class="input-field">
</td>
【问题讨论】:
-
它在 chrome 中也不起作用
-
抱歉,plunker 中有一个错误类型,但你是对的,innerHtml 和 innerText 在 chrome 上也不起作用,但 textContent 可以。
-
您的 plunker 在我的 Win10 笔记本电脑上的 Chrome 和 Firefox 中显示“Angular!v5.2.7”三次。
-
第一个带有innerHtml,第二个innerHtml和第三个textContent。
-
它们都是可编辑的。抱歉,我无法提供更好的服务,但我的个人生活真的很匆忙,因为这是我工作中的一个小问题,所以我优先考虑其他问题。
标签: html angular typescript firefox angular5