Currently utilizing Angular 6
.
I've established a custom directive named StickyPopover by extending NbgPopover and have integrated it into the declaration
of app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { AuthLayoutComponent } from './layouts/auth-layout/auth-layout.component';
import {AdminLayoutComponent} from './layouts/admin-layout/admin-layout.component';
import {FormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {StickyPopoverDirective} from './sticky-popover.directive';
@NgModule({
declarations: [
AppComponent,
AuthLayoutComponent,
AdminLayoutComponent,
StickyPopoverDirective // custom created directive
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
RouterModule,
NgbModule.forRoot(),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
The location of the directive exists at the same level as that of the app.module.ts file.
The content of sticky-popover.directive.ts is as follows:
import {
ElementRef,
Directive, Input, TemplateRef,
EventEmitter,
Renderer2,
Injector,
ComponentFactoryResolver,
ViewContainerRef,
NgZone, OnInit, OnDestroy
} from '@angular/core';
import { NgbPopover, NgbPopoverConfig } from '@ng-bootstrap/ng-bootstrap';
@Directive({
selector: '[appStickyPopover]',
exportAs: 'stickyPopover'
})
export class StickyPopoverDirective extends NgbPopover implements OnInit, OnDestroy {
@Input() stickyPopover: TemplateRef<any>;
popoverTitle: string;
placement: 'auto' | 'top' | 'bottom' | 'left' | 'right' | 'top-left' | 'top-right' | 'bottom-left' |
'bottom-right' | 'left-top' | 'left-bottom' | 'right-top' | 'right-bottom' | ('auto' | 'top' | 'bottom' |
'left' | 'right' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'left-top' | 'left-bottom' |
'right-top' | 'right-bottom')[];
triggers: string;
container: string;
shown: EventEmitter<{}>;
hidden: EventEmitter<{}>;
ngpPopover: TemplateRef<any>;
canClosePopover: boolean;
toggle(): void {
super.toggle();
}
isOpen(): boolean {
return super.isOpen();
}
constructor(
private _elRef: ElementRef,
private _render: Renderer2,
injector: Injector,
componentFactoryResolver: ComponentFactoryResolver,
private viewContainerRef: ViewContainerRef,
config: NgbPopoverConfig,
ngZone: NgZone
) {
super(_elRef, _render, injector, componentFactoryResolver, viewContainerRef, config, ngZone, document);
this.triggers = 'manual';
this.popoverTitle = 'Permissions';
this.container = 'body';
}
ngOnInit(): void {
super.ngOnInit();
this.ngbPopover = this.stickyPopover;
this._render.listen(this._elRef.nativeElement, 'mouseenter', () => {
this.canClosePopover = true;
this.open();
});
this._render.listen(this._elRef.nativeElement, 'mouseleave', (event: Event) => {
setTimeout(() => { if (this.canClosePopover) { this.close(); } }, 100);
});
this._render.listen(this._elRef.nativeElement, 'click', () => {
this.close();
});
}
ngOnDestroy(): void {
super.ngOnDestroy();
}
open() {
super.open();
const popover = window.document.querySelector('.popover');
this._render.listen(popover, 'mouseover', () => {
this.canClosePopover = false;
});
this._render.listen(popover, 'mouseout', () => {
this.canClosePopover = true;
setTimeout(() => { if (this.canClosePopover) { this.close(); } }, 0);
});
}
close() {
super.close();
}
}
I have a module called SavedSearches, which is imported within the AdminModule
, declared in the app.module.ts
When I try to use the StickyPopover directive within the template of the SavedSearches module like this:
<i class="fa fa-plus-circle" aria-hidden="true" appStickyPopover [popoverTitle]="additional" [autoClose]="true" data-placement="right"></i>
An error occurs stating:
can't bind to 'popoverTitle' since it isn't a known property of i
If I move the directive inside the SavedSearches module and include it in the saved-searches.module.ts declaration
, it works without errors. However, using it in other modules results in:
StickyPopovoerDirective is a part of the declaration of 2 modules. Move it in the upper module which imports these two modules.