I'm kinda new to Angular and am having issues integrating ngx-bootstrap in my project when using ng modules...
To be more brief - Everything works properly when I add html directly in app.component.html but won't work when I add the same code in app-component and declare it inside app.component.html
Steps taken :
Used Angular CLI 1.7.4 to generate a new project (with Angular 5.2.9)
Used
npm install --save bootstrap ngx-bootstrap
and imported bootstrap.min.css in my style.cssCreated a new module called 'app-bootstrap.module.ts' in src/app,and included the following in it :
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
@NgModule({
imports: [BsDropdownModule.forRoot()],
exports: [BsDropdownModule]
})
export class AppBootstrapModule { }
At this point, if I include the html code e.g. say for the dropdown button in
app.component.html
(after importing above module too), it works as expected. However, this is not what I want, so I went along with following steps.
- Created a new component in 'dropdown' under src/app and included the following in it
dropdown.component.html
:
<div class="btn-group" dropdown>
<button dropdownToggle type="button" class="btn btn-primary dropdown-toggle">
Button dropdown
<span class="caret"></span>
</button>
<ul *dropdownMenu class="dropdown-menu" role="menu">
<li role="menuitem">
<a class="dropdown-item" href="#">Action</a>
</li>
<li role="menuitem">
<a class="dropdown-item" href="#">Another action</a>
</li>
<li role="menuitem">
<a class="dropdown-item" href="#">Something else here</a>
</li>
<li class="divider dropdown-divider"></li>
<li role="menuitem">
<a class="dropdown-item" href="#">Separated link</a>
</li>
</ul>
</div>
- Created a module called
custom-components.module.ts
in src/app and declared dropdown component inside it :
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DropdownComponent } from './dropdown/dropdown.component';
@NgModule({
imports: [CommonModule],
declarations: [DropdownComponent],
exports: [DropdownComponent]
})
export class CustomComponentsModule { }
- Imported
customComponentsModule
inapp.module.ts
as follows :
//... Bootstrap and Components Modules
import { AppBootstrapModule } from './app-bootstrap.module';
import { CustomComponentsModule } from './custom-components.module';
@NgModule({
declarations: [AppComponent],
imports: [
CustomComponentsModule, //Custom Components
AppBootstrapModule //Bootstrap Module
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Added
<app-dropdown></app-dropdown>
insideapp.components.html
When I run
ng serve
here, the button shows up all styled up but when I click on it, nothing happens. As mentioned before, dropdown works when I put the same code inapp.component.html
- this lead me to believe there is some scope issue I am not getting. Frustratingly enough, no errors show up.
Note :
I've used a 'dropdown' component just for sake of brevity.
If I have missed adding something important, kindly ask for same in comments and I'll provide it.
Apologies for the big post; any help would be much appreciated, thank you!