【问题标题】:md-input binding error in Angular2 stable versionAngular2 稳定版中的 md-input 绑定错误
【发布时间】:2017-04-03 03:33:20
【问题描述】:

我在将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 和此solution2(相同)导入FormsModule,但仍然出现相同的错误。是否有其他解决方案?

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>

还有Module file

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 {}

有什么帮助吗?

【问题讨论】:

    标签: angular typescript angular-material2


    【解决方案1】:

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

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

    我强烈建议您通过Getting started page Angular2 material

    注意: 确保您应该在要使用Angular 2 material 组件的任何模块组件中导入MaterialModule

    【讨论】:

    • 但是我已经在app.moduleimport { MdInputModule } from '@angular2-material/input'; and it works in other pages :/ 中有这个了
    • @53l3m 您遇到此错误的组件,它是否属于ParModule
    • @53l3m 就是这个原因,你还没有导入MaterialModule表单ParModule
    猜你喜欢
    • 2017-08-28
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2016-12-20
    相关资源
    最近更新 更多