0

I am using GridStack (NPM) to build a dashboard in my Angular project. I want to dynamically set Angular components as GridStack widgets. I currently trying it this way, which doesn't work:

widget.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-widget',
  template: '<span>Widget (Angular Component)</span>',
})
export class WidgetComponent {}

demo.component.ts

import { Component, OnInit } from '@angular/core';
import { GridStack } from 'gridstack';

@Component({
  selector: 'app-demo',
  template: '<div class="grid-stack"></div>',
})
export class DemoComponent implements OnInit {
  ngOnInit() {
    const grid = GridStack.init();
    grid.load([
      {
        content: '<app-widget />',
      },
      { content: '<span>Widget (Native)</span>' },
    ]);
  }
}

This results in: Browser screenshot incl. DOM

Of course, the browser doesn't know how to render <app-widget />. So, <app-widget /> should be somehow pre-rendered here, in order to work. I tried it with DomSanitizer (from @angular/platform-browser) but I get the same result.

The question is: Does anybody know a way of setting an Angular component as content in a GridStack widget within code?

Thanks!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • This is how I tried it with _DomSanitizer_: `{content: this.domSanitizer.sanitize(SecurityContext.HTML, this.domSanitizer.bypassSecurityTrustHtml(''))!}` – Peter Holzer Aug 21 '21 at 05:39
  • When you add `` with GridStack it doesn't actually add Angular components, only html tags with that name. You might need to let Angular generate component. Take a look at this https://github.com/gridstack/gridstack.js/pull/1824 – Bojan Kogoj Aug 21 '21 at 07:16
  • @AviadP. You mean I should consider using [angular-gridster2](https://www.npmjs.com/package/angular-gridster2)? Wasn't aware of this, to be honest. I will definitely try it out. Many thanks! – Peter Holzer Aug 23 '21 at 05:19
  • 1
    Whenever someone asks a question about a library which is not suited for angular, the first thing I search for is if someone already created an angular adaptation - it's the most straightforward "non-wheel-reinventing" way – Aviad P. Aug 23 '21 at 05:22

0 Answers0