要在自定义组件中使用Angular Material的,需要确保自定义组件中的表单控件与位于同一个元素内。
以下是一个示例,展示了如何在自定义组件中使用:
位于同一个元素内:
This field is required
@Component装饰器中引入MatFormFieldModule:import { Component } from '@angular/core';
import { MatFormFieldModule } from '@angular/material/form-field';
@Component({
selector: 'app-custom-component',
templateUrl: './custom-component.component.html',
styleUrls: ['./custom-component.component.css'],
providers: [MatFormFieldModule]
})
export class CustomComponent {
// Your component code here
}
和所需的模块:
通过以上步骤,你应该能够在自定义组件中正确显示。确保在自定义组件中正确设置表单控件的验证以及的条件,以便根据需要显示错误消息。