你可以使用readonly
属性来禁用对输入框的编辑,并使用ngModel
指令绑定输入框的值。当输入框的内容发生改变时,你可以将其保存到一个变量中,然后在输入框中显示这个变量的值。下面是一个示例代码:
import { Component } from '@angular/core';
@Component({
selector: 'app-input-example',
template: `
`,
})
export class InputExampleComponent {
inputValue: string = '初始内容';
constructor() { }
}
在上面的示例中,我们使用[readonly]="true"
来禁用输入框的编辑功能。[(ngModel)]="inputValue"
将输入框的值绑定到了inputValue
变量上。你可以在组件中使用这个变量来存储输入框的值,并在需要时显示它。
注意:如果你只想禁用对当前值的编辑,但仍允许用户删除内容并输入新内容,可以使用[readonly]="false"
来启用编辑功能。