Angular 2材料设计组件是否支持布局指令?

Mar*_*the 56 flexbox material-design angular

我正在尝试使用Angular2 Material Design组件,我无法使任何布局指令起作用.例:

根据这些例子,这应该"正常":

<div layout="row">
  <div flex>First item in row</div>
  <div flex>Second item in row</div>
</div>
<div layout="column">
  <div flex>First item in column</div>
  <div flex>Second item in column</div>
</div>
Run Code Online (Sandbox Code Playgroud)

但它没有 - 它只是将页面上的元素呈现为普通的旧div.(我正在使用最新版本的Chrome).

我错过了什么,比如有一个我应该导入的CSS文件吗?

Abd*_*yer 122

2017年1月更新:

Angular 2团队最近为布局添加了新的NPM包flex-layout.它是一个独立的包装,独立于角形材料.
完整说明可在github页面自述文件中找到.

安装模块:

npm install @ angular/flex-layout -save

在app.module.ts(或等效的)中,声明模块:

import {FlexLayoutModule} from "@angular/flex-layout";

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

标记示例:

<div class="flex-container" 
     fxLayout="row" 
     fxLayout.xs="column"
     fxLayoutAlign="center center"
     fxLayoutAlign.xs="start">
  <div class="flex-item" fxFlex="20%" fxFlex.xs="40%">  </div>
  <div class="flex-item" fxFlex>        </div>
  <div class="flex-item" fxFlex="25px"> </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是一个从flex-layout github页面中获取的plunker样本.


原答案:

您指的文档适用于angular1材质.Angular2材质仍然没有任何布局指令.

您可以通过简单的方式轻松创建指令.

所有你必须知道的:

layout="row"style="display:flex;flex-direction:row"
layout="column"=> 相同style="display:flex;flex-direction:column"

并且flex等于style="flex:1"

作为指令:

@Directive({
  selector:'[layout]'
})
export class LayoutDirective{
  @Input() layout:string;
  @HostBinding('style.display') display = 'flex';

  @HostBinding('style.flex-direction')
  get direction(){
       return (this.layout === 'column') ? 'column':'row';
  }
}
Run Code Online (Sandbox Code Playgroud)

flex指令,使用它:<div flex><div flex="10">0到100%之间的任何数字.另外,为了好玩,我增加了收缩和增长投入

@Directive({
  selector:'[flex]'
})
export class FlexDirective{
    @Input() shrink:number = 1;
    @Input() grow:number = 1;
    @Input() flex:string;

    @HostBinding('style.flex')
    get style(){
        return `${this.grow} ${this.shrink} ${this.flex === '' ? '0':this.flex}%`;
    }
}
Run Code Online (Sandbox Code Playgroud)

要在各处使用它们而不将它们添加到每个组件:

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent,FlexDirective ,LayoutDirective ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

这里是一个样本普拉克

  • 作为对材料库不熟悉的人,这让我感到头疼了一段时间......很难说出哪些指令只是NG2. (3认同)
  • 那个模块上不存在`forRoot` ... https://github.com/angular/flex-layout/wiki/Integration-with-Angular-CLI (2认同)