Pankaj Parkar
Indian | 30 | Ex MVP | Angular GDE
<my-elemnent drag hightlight>
</my-elemnent>
@pankajparkar
www.pankajparkar.com
@Directive({
selector: '[appBlink]',
standalone: true,
})
export class BlinkDirective {
@HostBinding('style.animation')
animation = 'blinker 1s step-start infinite';
@Input() theme = 'red';
@Output() elementClick = new EventEmitter<MouseEvent>();
@HostListener('click', ['$event'])
click(event: MouseEvent) {
this.elementClick.emit(event);
}
}
@pankajparkar
www.pankajparkar.com
@Directive({
selector: '[appBold]',
standalone: true
})
export class BoldDirective {
@HostBinding('style.font-weight')
bold = 'bolder';
}
My Text
My Text
@pankajparkar
www.pankajparkar.com
@Component({
selector: 'app-ribbon',
standalone: true,
imports: [CommonModule],
template: `
<div class="ribbon"
[ngClass]="placement ?? 'ribbon-top-left'">
<span>sale</span>
</div>
`,
styles: [`...`]
})
export class RibbonComponent {
@Input() placement: string | null | undefined
= 'ribbon-top-right';
}
@pankajparkar
www.pankajparkar.com
Applying directly on component
<app-ribbon appBold appBlink>
</app-ribbon>
@pankajparkar
www.pankajparkar.com
@Component({
selector: 'app-ribbon',
standalone: true,
...,
})
export class RibbonComponent extends BoldDirective {
...
}
Apply single directive
@pankajparkar
www.pankajparkar.com
@Component({
selector: 'app-ribbon',
standalone: true,
...,
})
export class RibbonComponent extends WithBoldAndBlink {
...
}
class WithBold extends BoldDirective {
}
class WithBoldAndBlink extends BlinkDirective {
}
Apply multiple directive
Principal Application Devloper
pankajparkar
#devwhorun 🏃♂️ - Feb 100km approx
@Component({
selector: 'my-component',
standalone: true,
template: `<p>My Component</p>`
hostDirectives: [MyDirective],
imports: [],
})
export class MyComponent { }
@Component({
selector: 'app-ribbon',
standalone: true,
imports: [],
hostDirectives: [
BlinkDirective
BoldDirective,
],
template: `
<div class="ribbon"
[ngClass]="placement ?? 'ribbon-top-left'">
<span>sale</span>
</div>
`,
styles: []
})
export class RibbonComponent {... }
<app-ribbon />
@Component({
selector: 'app-ribbon',
standalone: true,
imports: [],
hostDirectives: [
{
directive: BlinkDirective,
inputs: ['theme: bannerTheme'],
outputs: ['elementClick: bannerClick'],
},
BoldDirective,
],
template: `...`,
styles: []
})
export class RibbonComponent { }
<app-ribbon
bannerTheme="'black'"
(bannerClick)="bannerClick($event)" />
@Component({
selector: 'my-menu-item',
standalone: true,
imports: [],
hostDirectives: [CdkMenuItem],
template: `
<ng-content></ng-content>
`,
})
export class MenuItemComponent { }
@Component({
selector: 'my-menu',
standalone: true,
imports: [],
hostDirectives: [CdkMenu],
template: `
<ng-content></ng-content>
`,
})
export class MenuComponent { }
<button [cdkMenuTriggerFor]="menu">
Click me!
</button>
<ng-template #menu>
<my-menu>
<my-menu-item>Tes M</my-menu-item>
<my-menu-item>Tes M</my-menu-item>
<my-menu-item>Tes M</my-menu-item>
</my-menu>
</ng-template>
<select>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
www.pankajparkar.com
@pankajparkar
www.pankajparkar.com
@pankajparkar
@pankajparkar
www.pankajparkar.com
By Pankaj Parkar
Directive Composition API