确保已将placeholder属性添加到select元素,如下所示:
确定样式是否影响占位符的可见性。使用浏览器开发者工具检查CSS,并尝试更改样式以使占位符可见。
如果仍未解决问题,可以使用以下Angular指令来指定占位符样式: import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: 'select[placeholder]' })
export class PlaceholderDirective { constructor(private el: ElementRef) {} ngOnInit() { this.el.nativeElement.classList.add('placeholder'); } }
在HTML中使用指令:
为指令定义CSS样式: select[placeholder].placeholder, select.placeholder{ color: gray; }