Angular2材质md按钮错误

时间:2016-08-13 00:30:03

标签: javascript angularjs angular typescript angular-material2

我之前看到Angular2推出了RC5,所以我决定更新我的一个测试应用程序以查看更改以及如何调整。这个应用程序正在使用Material2,我也将其更新为Alpha 7.2,但是在尝试使用md-button组件时出现此错误

  

“模板解析错误:无法绑定到'md-ripple-trigger',因为它   不是'div'的已知属性。 ( “* ngIf =” isRippleEnabled()”   类= “MD-按钮纹波”   [class.md-button-ripple-round] =“isRoundButton()”[错误    - >] [md-ripple-trigger] =“getHostElement()”[md-ripple-color] =“isRoundButton()?'rgba(255,255,255,0.2)”):   MdButton @ 0:180无法绑定到'md-ripple-color',因为它不是已知的   'div'的财产。 (“吨纹波”   [class.md按钮纹波轮] = “isRoundButton()”   [md-ripple-trigger] =“getHostElement()”[错误    - >] [md-ripple-color] =“isRoundButton()?'rgba(255,255,255,0.2)':''”md-ripple-background-color =“rgb”):MdButton @ 0: 219"

我正在尝试添加一个名为nav-bar的新组件上使用它,这里是添加了材料的Angular CLI创建的文件

TypeScript文件

import { Component, OnInit } from '@angular/core';
import { MdToolbar } from '@angular2-material/toolbar';
import { MdButton } from '@angular2-material/button';
import { MdIcon, MdIconRegistry } from '@angular2-material/icon';

@Component({
  moduleId: module.id,
  selector: 'nav-bar',
  templateUrl: 'nav-bar.component.html',
  styleUrls: ['nav-bar.component.css'],
  directives: [ 
    MdToolbar, 
    MdButton,
    MdIcon 
  ],
  providers: [ 
    MdIconRegistry 
  ]
})

export class NavBarComponent implements OnInit {

  constructor() {}

  ngOnInit() {
  }

}

HTML文件

<md-toolbar>
  <button md-button>
    <md-icon>menu</md-icon>
  </button>
  NavBar
</md-toolbar>

1 个答案:

答案 0 :(得分:9)

尝试在MdRippleModule中导入AppModule

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { HttpModule} from '@angular/http';
import { MdRippleModule } from '@angular2-material/core/core'; <== this line

@NgModule({
  imports:      [ BrowserModule, HttpModule, MdRippleModule ], <== add here
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

或者在组件中传递MdRipple指令:

...
import { MdRipple } from '@angular2-material/core/core'; <== this line
@Component({
  moduleId: module.id,
  selector: 'nav-bar',
  templateUrl: 'nav-bar.component.html',
  styleUrls: ['nav-bar.component.css'],
  directives: [ 
    MdToolbar, 
    MdButton,
    MdIcon,
    MdRipple <== add here
  ],
  providers: [ 
    MdIconRegistry 
  ]
})
export class NavBarComponent implements OnInit {
...