Skip to content

mat-grid-list with dynamic column count and ssr initial page load #9615

@goelinsights

Description

@goelinsights

Bug, feature request, or proposal:

Not sure: it's an error I'm struggling to debug. Hoping I'm missing something obvious

What is the expected behavior?

mat-grid-list compiles with Angular Universal

What is the current behavior?

Receiving the following error on initial page load on the route, which crashes the app... (but if I load the app on a page without the grid and then navigate to the page it works fine)

ERROR Error: mat-grid-list: must pass in number of columns. Example:
at Error (native)
at MatGridList._checkCols (/Users/vijay/ng2_projects/ng-bite-ssr/dist/server.js:136175:19)
at MatGridList.ngOnInit (/Users/vijay/ng2_projects/ng-bite-ssr/dist/server.js:136145:14)
at checkAndUpdateDirectiveInline (/Users/vijay/ng2_projects/ng-bite-ssr/dist/server.js:12846:19)
at checkAndUpdateNodeInline (/Users/vijay/ng2_projects/ng-bite-ssr/dist/server.js:14370:20)
at checkAndUpdateNode (/Users/vijay/ng2_projects/ng-bite-ssr/dist/server.js:14313:16)
at prodCheckAndUpdateNode (/Users/vijay/ng2_projects/ng-bite-ssr/dist/server.js:15037:5)

What are the steps to reproduce?

  1. Start with Angular Universal Starter
  2. Add Material Module and add MatGridList to a component
  3. use a function to define the # of columns, e.g.,
<mat-grid-list [cols]="calcGridColumns(mediaSize)"> ... </mat-grid-list>
  
...
calcGridColumns(size) {
    switch (size) {
      case 'xl': return 8;
      case 'lg': return 8;
      case 'md': return 6;
      case 'sm': return 4;
      case 'xs': return 3;
      default: return 3;
    }
  }
  1. Use build steps defined in Step 5 of angular cli story on universal https://github.com/angular/angular-cli/wiki/stories-universal-rendering

What is the use-case or motivation for changing an existing behavior?

App compiles using universal

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

"@angular/common": "5.2.2",
"@angular/material": "5.0.4",

Is there anything else we should know?

Looks like I'm triggering this code:

https://github.com/angular/material2/blob/master/src/lib/grid-list/grid-list.ts
/** Throw a friendly error if cols property is missing */
  private _checkCols() {
    if (!this.cols) {
      throw Error(`mat-grid-list: must pass in number of columns. ` +
                  `Example: <mat-grid-list cols="3">`);
    }
  }

I think this means that with SSR, [cols]="function(value)" = undefined despite the default value in the switch statement. Trying to understand why thats the case and how to fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions