Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'ngStyle' since it isn't a known property of 'div' when CommonModule has been imported

Tags:

angular

I have a component:

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

@Component({
  selector: 'my-menu',
  templateUrl: './my-menu.component.html'
})
export class MenuComponent {
  //some code here
}

I use that component in multiple modules, so that's why I have a SharedModule in which I export it:

import { NgModule } from '@angular/core';
import { MenuComponent } from './my-menu/my-menu.component';
import { CommonModule } from '@angular/common';

@NgModule({
  imports: [CommonModule],
  declarations: [ MenuComponent ],
  exports: [ MenuComponent, CommonModule ],
})
export class SharedModule {}

And I use the SharedModule in multiple modules that are basically similar to this:

import { TestComponent } from './test.component';
import { Injector, DoBootstrap, NgModule } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { BrowserModule } from '@angular/platform-browser';
import { SharedModule } from '../shared/shared.module';
import { CommonModule } from '@angular/common';

@NgModule({
  declarations: [TestComponent],
  imports: [BrowserModule, FormsModule, HttpClientModule, SharedModule, CommonModule],
  entryComponents: [TestComponent],
  providers: [],
  exports: []
})
export class TestModule implements DoBootstrap {
  constructor(private injector: Injector) { }

  ngDoBootstrap() {
    const ngElement = createCustomElement(TestComponent, { injector: this.injector, });
    customElements.get('my-test') || customElements.define('my-test', ngElement);
  }
}

However when I run the ng build command, I get the error: Can't bind to 'ngStyle' since it isn't a known property of 'div', which happens in file my-menu.component.html. This component has already been working correctly before I added the shared module. What am I doing wrong?

like image 420
petko_stankoski Avatar asked Nov 29 '25 02:11

petko_stankoski


2 Answers

You need to export CommonModule from SharedModule.

import { NgModule } from '@angular/core';
import { MenuComponent } from './my-menu/my-menu.component';
import { CommonModule } from '@angular/common';

@NgModule({
  imports: [CommonModule],
  declarations: [ MenuComponent ],
  exports: [ MenuComponent, CommonModule ], //add common module here
})
export class SharedModule {}
like image 151
Gourav Garg Avatar answered Dec 02 '25 05:12

Gourav Garg


you do not import the CommonModule module.

import { CommonModule } from '@angular/common';
like image 33
Hamada Avatar answered Dec 02 '25 05:12

Hamada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!