angular - Angular2 稳定版中的 md-input 绑定(bind)错误

标签 angular typescript angular-material2

我在将 angular2 应用程序升级到稳定版本时遇到了一个常见错误,但上述解决方案均不适用于我(这几乎是一个普遍的唯一解决方案)。 这是它:

Template parse errors:
Can't bind to 'placeholder' since it isn't a known property of 'md-input'.
1. If 'md-input' is an Angular component and it has 'placeholder' input, then verify that it is part of this module.
2. If 'md-input' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.

我已尝试根据此 solution1 导入 FormsModule还有这个solution2 (相同)但仍然出现相同的错误。是否有其他解决方案?

HTML :

  <div class="col-lg-10 col-lg-offset-1">
      <div class="md-block">
        <md-input
          id="name"
          [ngModel]="xyzStuff?.xyz_name"
          placeholder="{{'xyzName' | translate}}"
          formControlName="name"
          dividerColor="{{getDividerColor(xyzForm.controls.name)}}"
        >
        </md-input>
      </div>
    </div>

模块文件:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { UIRouterModule } from 'ui-router-ng2';
import { TranslateModule } from 'ng2-translate';

import { ParComponent } from './par.component';
import { XyzSettingsComponent } from './shared/xyz-settings/xyz-settings.component';

@NgModule({
  declarations: [
    ParComponent,
    XyzSettingsComponent
  ],
  imports:       [
    FormsModule,
    BrowserModule,
    UIRouterModule,
    TranslateModule,
    ReactiveFormsModule
  ],
  providers:     [],
  exports:       [
    FormsModule,
    BrowserModule,
    UIRouterModule,
    TranslateModule,
    ReactiveFormsModule
  ]
})
export class ParModule {}

有什么帮助吗?

最佳答案

md-input 属于angular2 Material 项目。 Angular 2 Material 是完全不同的插件。您必须确保导入 MaterialModule 导入并注入(inject)到 NgModuleimports 选项中。

import { MaterialModule } from '@angular/material';

@NgModule({
  declarations: [
    ...
  ],
  imports:       [
    ...,
    MaterialModule.forRoot(), //<-- imported material module here
    ...
  ],
  providers:     [],
  exports:       [
    ...
  ]
})

我强烈建议您查看 Getting started page Angular2 material

Note: Make sure you should import MaterialModule in whichever modules component you want to use Angular 2 material components.

关于angular - Angular2 稳定版中的 md-input 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691371/

相关文章:

Angular MatTabChangeEvent 不会在页面加载时触发

javascript - Angular 2 在 View /模板中调用函数

node.js - Platypi 中 ViewControl 的注册函数放在哪里

Angular2 Material Grid 全高

Angular: "cannot find name",元素在 DOM 中尚不存在

Angular 集成谷歌驱动器

c# - 如何在多个 visual studio 项目中组织我们的 javascript

angular - 如何使用 Angular Material snackbar 检查解雇原因?

javascript - Angular2 Material 中的 Md-ripple

javascript - 如何在 Angular 2 中使用 require 加载外部 css