要在Angular 7移动可编辑div中插入插入符号,您可以使用以下步骤:
@ViewChild
装饰器来实现这一点。例如:import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponentComponent implements AfterViewInit {
@ViewChild('editableDiv') editableDiv: ElementRef;
constructor() { }
ngAfterViewInit() {
const divElement: HTMLDivElement = this.editableDiv.nativeElement;
// 在这里可以对divElement进行操作,比如插入插入符号
}
}
ngAfterViewInit
生命周期钩子中,您可以获取可编辑div元素并进行操作。例如,要在div中插入插入符号,您可以使用document.execCommand()
方法:ngAfterViewInit() {
const divElement: HTMLDivElement = this.editableDiv.nativeElement;
divElement.focus();
document.execCommand('insertText', false, '|');
}
以上代码将在可编辑div中插入一个竖线字符作为插入符号。您还可以根据需要更改插入的符号。
希望这可以帮助到您!