可以通过在 Angular 的表单控件中使用 ngModel 绑定来设置输入框的默认值。例如:
在组件中定义数据模型:
@Component({ selector: 'app-example', templateUrl: './example.component.html' }) export class ExampleComponent implements OnInit { defaultInputValue = '默认值'; }
在模板文件中使用 ngModel 绑定表单控件:
这样就能够在页面加载时显示设置的默认值了。注意设置的默认值要在组件的 ngOnInit 生命周期钩子中被赋值。