【问题标题】:Responsive Design using md-grid-list in angular 2在角度 2 中使用 md-grid-list 的响应式设计
【发布时间】:2017-10-09 01:33:12
【问题描述】:

我正在查看 Angular 2 中 md-grid-list 的基本示例

HTML 代码:

<md-grid-list cols="4" rowHeight="100px">
   <md-grid-tile *ngFor="let tile of tiles"
         [colspan]="tile.cols"
         [rowspan]="tile.rows"
         [style.background]="tile.color">
         {{tile.text}}
    </md-grid-tile>
</md-grid-list>

TS 代码:

export class GridListDynamicExample {
  tiles = [
    {text: 'One', cols: 3, rows: 1, color: 'lightblue'},
    {text: 'Two', cols: 1, rows: 2, color: 'lightgreen'},
    {text: 'Three', cols: 1, rows: 1, color: 'lightpink'},
    {text: 'Four', cols: 2, rows: 1, color: '#DDBDF1'},
  ];
}

上面的代码结果如下: 如何使用一些 HTML 指令或 CSS 将布局设​​置为“列”,即列“二”以在较小的屏幕尺寸上低于行(一和四)?

Angular 1 中的 Angular Material 可以选择通过指定 "md-cols-xs="1" md-cols-sm="2" md-cols-md="4" md-cols-gt-md= "6" "。

【问题讨论】:

  • 这是使用仍处于测试阶段的框架的缺点之一。响应式网格列表尚未实现。

标签: css angular responsive-design angular-material2


【解决方案1】:

您可以将指令添加到您的组件,然后像这样在指令中执行工作;

import { Directive, ElementRef, Input, HostListener, Output } from '@angular/core';
import * as _ from  "lodash";
@Directive({ selector: '[myResponsive]' })

export class TestDirective {
  @Input() tiles;

  @HostListener('window:resize', ['$event'])
  onResize(event) {
    if (event.target.innerWidth < 980) {
      _.each(this.tiles, tile => {
        tile.cols = 2;
        tile.rows = 1;
      });
    } else {
      this.tiles = [
        {text: 'One', cols: 3, rows: 1, color: 'lightblue'},
        {text: 'Two', cols: 1, rows: 2, color: 'lightgreen'},
        {text: 'Three', cols: 1, rows: 1, color: 'lightpink'},
        {text: 'Four', cols: 2, rows: 1, color: '#DDBDF1'}
      ];
    }
  }

  constructor(el: ElementRef) {

  }
}

您还需要将指令添加到 app.module.ts

import { TestDirective } from "./test.directive";
@NgModule({
  imports: [
      ...
  ],
  declarations: [
     TestDirective
  ]

你的 HTML 应该是这样的

<md-grid-list cols="4" rowHeight="100px">
   <md-grid-tile myResponsive [(tiles)]="tiles" *ngFor="let tile of tiles"
         [colspan]="tile.cols"
         [rowspan]="tile.rows"
         [style.background]="tile.color">
         {{tile.text}}
    </md-grid-tile>
</md-grid-list>

【讨论】:

  • 这如何适应使用相同指令的多个网格列表?似乎您已将默认值编码到指令中,这些默认值可能适用于也可能不适用于第二和第三网格列表。
  • 我不太明白您的问题,但如果其他网格彼此分开,您可以为每个网格添加相同的指令。答案的目的是您可以使用指令来获取屏幕宽度并使用它来更改您的 html。如果您不使用 css 使其响应。它只是动态更改 col 和 row span,因此您可以使用相同的指令更改所有其他网格。
【解决方案2】:

这是你可以达到的最简单的方法:)

your.component.html

<md-card class="sub-category-grid" style="padding:0px;" (window:resize)="onResize($event)">

  <md-grid-list cols="{{test}}" rowHeight="1:1">
     <md-grid-tile *ngFor="let item of Items"> 
       {{item.title}}
    </md-grid-tile>
 </md-grid-list>

</md-card>

your.component.ts

// init this var with the default number of columns
test: any = 2;

Items: any = [
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" },
    {title: "title", img_src: "../../../assets/img/-samples/raketa.png", description: "Description" }
  ]


constructor() { }

ngOnInit() {

}


onResize(event) {
    const element = event.target.innerWidth;
    console.log(element);


    if (element < 950) {
      this.test = 2;
    }

    if (element > 950) {
      this.test = 3;
    }

    if (element < 750) {
      this.test = 1;
    }
  }

【讨论】:

  • 确实比其他答案更干净。谢谢
【解决方案3】:

据我了解,响应式部分目前位于 flex-layout 项目中。该库中的一些常用实用程序将移至材料已经使用的 angular/cdk。 Flex-layout 项目提供了一个 observable,您可以订阅它以获取有关断点更改的通知 - ObservableMedia。您还可以使用 MediaService 服务(也来自 flex-layout)来声明窗口大小。

因此,此代码可以这样实现。请注意,当切换发生时,我使用 trackBy 函数来保留原始框。

    export class AppComponent  {
      tiles: Array<Object>;
      public columns = 4;
      private subscription: Subscription;

      tilesBig = [
        {text: 'One', cols: 3, rows: 1, color: 'lightblue', id: 1},
        {text: 'Two', cols: 1, rows: 2, color: 'lightgreen', id: 2},
        {text: 'Three', cols: 1, rows: 1, color: 'lightpink', id: 3},
        {text: 'Four', cols: 2, rows: 1, color: '#DDBDF1', id: 4},
      ];

      tilesSm = [
        {text: 'One', cols: 3, rows: 1, color: 'lightblue', id: 1},
        {text: 'Three', cols: 1, rows: 1, color: 'lightpink', id: 3},
        {text: 'Four', cols: 2, rows: 1, color: '#DDBDF1', id: 4},
        {text: 'Two', cols: 3, rows: 1, color: 'lightgreen', id: 2},
      ];

      constructor(private _media$: ObservableMedia,
                  private mediaService: MediaService) {
        this.subscription = this._media$.subscribe((e: MediaChange) => {
          this.toggle();
        });
      }

      ngOnInit() {
        this.toggle();
      }

      private toggle() {
        const isSmall = this.mediaService.isActive('lt-md');
        this.columns = isSmall ? 3 : 4;
        this.tiles = isSmall ? this.tilesSm : this.tilesBig;
      }

      trackById(index: number, item: any): string { return item['id']; }
    }

可以看代码https://stackblitz.com/edit/angular-t325tj?embed=1&file=app/app.component.ts

【讨论】:

    【解决方案4】:

    在 Angular 4 中进行响应式设计并不像在 bootstrape 中那么简单。要使 md-grid-list 响应式或应该根据设备宽度更改视图,那么我们需要使用 flex 布局库

    为了清楚地了解响应式事物在角度访问下面的链接中是如何工作的

    访问http://brianflove.com/2017/05/03/responsive-angular/
    演示http://run.plnkr.co/preview/cja10xr7900083b5wx6srd0r6/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-19
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2017-10-07
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多