在HTML模板中使用属性绑定,将占位符值绑定到组件中的变量。然后,在onBlur事件处理程序中,使用变量而不是硬编码的字符串。
示例代码如下:
HTML模板:
组件类:
import { Component } from '@angular/core';
@Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent { myPlaceholder = 'Enter your text';
onBlur() { console.log('Placeholder value:', this.myPlaceholder); } }
下一篇:Angular如何防止缓存?