如何在Angular Material 2中使用<md-error>?

Wen*_*uen 8 angular-material angular-material2 angular

当Angular Material 2中的所需输入无效时,我试图显示错误消息.Angular Material演示应用程序中有一个示例:

<form novalidate>
  <h4>Inside a form</h4>

  <md-input-container>
    <input mdInput name="example" placeholder="example"
      [(ngModel)]="errorMessageExample3" required>
    <md-error>This field is required</md-error>
  </md-input-container>

  <button color="primary" md-raised-button>Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)

预期结果

不幸的是,当我尝试做同样的事情时,我得到:

Unhandled Promise rejection: Template parse errors:
'md-error' is not a known element:
1. If 'md-error' is an Angular component, then verify that it is part 
   of this module.
2. If 'md-error' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' 
   to the '@NgModule.schemas' of this component to suppress this message.
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能使用<md-error>

fre*_*rik 6

你有没有加入MaterialModuleappModule?

@NgModule({
    imports: [
        ...
        MaterialModule
    ],
    ...
})
Run Code Online (Sandbox Code Playgroud)

更新:

md-error是一个尚未发布的新指令(截至测试版2),我猜它可能是下一版本的一部分.更多信息可以在这里找到:https://github.com/angular/material2/pull/3560

同时你可以使用md-hint.

  • 现在,Angular Material 2 1.0.0-beta-3中提供了md-error指令.请参阅https://material.angular.io/components/component/input. (2认同)