@telperion/ng-pack 0.1.3 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -56,7 +56,7 @@ export class SettingsComponent {
|
|
|
56
56
|
}
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
[Full documentation →](
|
|
59
|
+
[Full documentation →](https://github.com/telperiontech/telperion/tree/main/libs/ng-pack/storage-signals)
|
|
60
60
|
|
|
61
61
|
---
|
|
62
62
|
|
|
@@ -74,9 +74,57 @@ Signal-based forms utilities for Angular template-driven forms.
|
|
|
74
74
|
|
|
75
75
|
**Import:** `@telperion/ng-pack/utils`
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
Angular utility functions and plugins for enhanced development experience.
|
|
78
|
+
|
|
79
|
+
#### Key Features
|
|
80
|
+
|
|
81
|
+
- 🎯 Event modifier syntax for templates (`.pd`, `.sp`)
|
|
82
|
+
- 🔗 Directive-as-service provider utility
|
|
83
|
+
- 📦 Tree-shakeable and type-safe
|
|
84
|
+
- ⚡ Zero dependencies
|
|
85
|
+
|
|
86
|
+
#### Quick Start
|
|
87
|
+
|
|
88
|
+
**Event Modifiers Plugin**
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { provideEventModifiersPlugin } from '@telperion/ng-pack/utils';
|
|
92
|
+
|
|
93
|
+
bootstrapApplication(AppComponent, {
|
|
94
|
+
providers: [provideEventModifiersPlugin()]
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```html
|
|
99
|
+
<!-- Prevent default and stop propagation with modifiers -->
|
|
100
|
+
<form (submit.pd)="onSubmit()">...</form>
|
|
101
|
+
<div (click.sp)="handleClick()">...</div>
|
|
102
|
+
<button (click.pd.sp)="handleButtonClick()">Click me</button>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Provide Service Directive**
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { provideServiceDirective } from '@telperion/ng-pack/utils';
|
|
109
|
+
|
|
110
|
+
// Create an injection token
|
|
111
|
+
export const ParentService = new InjectionToken<ParentDirective>("ParentService");
|
|
112
|
+
|
|
113
|
+
// Provide directive as service
|
|
114
|
+
@Directive({
|
|
115
|
+
selector: '[parent]',
|
|
116
|
+
providers: [provideServiceDirective(ParentService, ParentDirective)],
|
|
117
|
+
})
|
|
118
|
+
export class ParentDirective { }
|
|
119
|
+
|
|
120
|
+
// Inject in child
|
|
121
|
+
@Directive({ selector: '[child]' })
|
|
122
|
+
export class ChildDirective {
|
|
123
|
+
private parent = inject(ParentService);
|
|
124
|
+
}
|
|
125
|
+
```
|
|
78
126
|
|
|
79
|
-
|
|
127
|
+
[Full documentation →](https://github.com/telperiontech/telperion/tree/main/libs/ng-pack/utils)
|
|
80
128
|
|
|
81
129
|
---
|
|
82
130
|
|
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.5", ngImport: i0, type: UtilsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
6
|
-
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "21.1.5", ngImport: i0, type: UtilsModule });
|
|
7
|
-
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "21.1.5", ngImport: i0, type: UtilsModule });
|
|
8
|
-
}
|
|
9
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.5", ngImport: i0, type: UtilsModule, decorators: [{
|
|
10
|
-
type: NgModule,
|
|
11
|
-
args: [{
|
|
12
|
-
imports: [],
|
|
13
|
-
}]
|
|
14
|
-
}] });
|
|
2
|
+
import { forwardRef, Inject, Injectable } from '@angular/core';
|
|
3
|
+
import { DOCUMENT } from '@angular/common';
|
|
4
|
+
import { EventManagerPlugin, EVENT_MANAGER_PLUGINS } from '@angular/platform-browser';
|
|
15
5
|
|
|
16
6
|
/**
|
|
17
7
|
* Utility function to create a provider for a directive/component that can be injected as a service.
|
|
@@ -61,9 +51,76 @@ function provideServiceDirective(token, directive) {
|
|
|
61
51
|
};
|
|
62
52
|
}
|
|
63
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Angular Event Manager Plugin that enables event modifier syntax in templates.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```html
|
|
59
|
+
* <form (submit.pd)="onSubmit()">...</form>
|
|
60
|
+
* <div (click.sp)="handleClick()">...</div>
|
|
61
|
+
* <button (click.pd.sp)="handleButtonClick()">Click me</button>
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
/**
|
|
65
|
+
* Event Manager Plugin that adds support for event modifiers.
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
class EventModifiersPlugin extends EventManagerPlugin {
|
|
69
|
+
/**
|
|
70
|
+
* Supported modifiers: `pd` (preventDefault), `sp` (stopPropagation)
|
|
71
|
+
*/
|
|
72
|
+
static modifierMap = {
|
|
73
|
+
'pd': (e) => e.preventDefault(),
|
|
74
|
+
'sp': (e) => e.stopPropagation(),
|
|
75
|
+
};
|
|
76
|
+
constructor(doc) {
|
|
77
|
+
super(doc);
|
|
78
|
+
}
|
|
79
|
+
supports(eventName) {
|
|
80
|
+
return eventName.includes('.') &&
|
|
81
|
+
eventName.split('.').some(part => part in EventModifiersPlugin.modifierMap);
|
|
82
|
+
}
|
|
83
|
+
addEventListener(element, eventName, handler) {
|
|
84
|
+
const parts = eventName.split('.');
|
|
85
|
+
const domEventName = parts[0];
|
|
86
|
+
const modifiers = parts.slice(1).filter(m => m in EventModifiersPlugin.modifierMap);
|
|
87
|
+
const wrappedHandler = (event) => {
|
|
88
|
+
modifiers.forEach(mod => EventModifiersPlugin.modifierMap[mod](event));
|
|
89
|
+
return handler(event);
|
|
90
|
+
};
|
|
91
|
+
element.addEventListener(domEventName, wrappedHandler);
|
|
92
|
+
return () => element.removeEventListener(domEventName, wrappedHandler);
|
|
93
|
+
}
|
|
94
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.5", ngImport: i0, type: EventModifiersPlugin, deps: [{ token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
95
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.1.5", ngImport: i0, type: EventModifiersPlugin });
|
|
96
|
+
}
|
|
97
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.5", ngImport: i0, type: EventModifiersPlugin, decorators: [{
|
|
98
|
+
type: Injectable
|
|
99
|
+
}], ctorParameters: () => [{ type: Document, decorators: [{
|
|
100
|
+
type: Inject,
|
|
101
|
+
args: [DOCUMENT]
|
|
102
|
+
}] }] });
|
|
103
|
+
/**
|
|
104
|
+
* Provides the Event Modifiers Plugin.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* bootstrapApplication(AppComponent, {
|
|
109
|
+
* providers: [provideEventModifiersPlugin()]
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
function provideEventModifiersPlugin() {
|
|
114
|
+
return {
|
|
115
|
+
provide: EVENT_MANAGER_PLUGINS,
|
|
116
|
+
useClass: EventModifiersPlugin,
|
|
117
|
+
multi: true
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
64
121
|
/**
|
|
65
122
|
* Generated bundle index. Do not edit.
|
|
66
123
|
*/
|
|
67
124
|
|
|
68
|
-
export {
|
|
125
|
+
export { provideEventModifiersPlugin, provideServiceDirective };
|
|
69
126
|
//# sourceMappingURL=telperion-ng-pack-utils.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telperion-ng-pack-utils.mjs","sources":["../../utils/src/
|
|
1
|
+
{"version":3,"file":"telperion-ng-pack-utils.mjs","sources":["../../utils/src/provide-service-directive.ts","../../utils/src/event-modifiers.plugin.ts","../../utils/src/telperion-ng-pack-utils.ts"],"sourcesContent":["import { forwardRef, InjectionToken, Provider, Type } from \"@angular/core\";\r\n\r\n/**\r\n * Utility function to create a provider for a directive/component that can be injected as a service.\r\n * This is useful for cases where you want to inject a directive/component instance into another directive/component.\r\n *\r\n * Example usage:\r\n *\r\n * files:\r\n * - parent.directive.ts\r\n * - child.directive.ts\r\n * - parent.service.ts\r\n *\r\n * parent.service.ts:\r\n * ```\r\n * import { InjectionToken } from \"@angular/core\";\r\n * import type { ParentDirective } from \"./parent.directive\";\r\n *\r\n * export const ParentService = new InjectionToken<ParentDirective>(\"ParentService\");\r\n * ```\r\n *\r\n * parent.directive.ts:\r\n * ```\r\n * @Directive({\r\n * selector: 'parent',\r\n * providers: [provideServiceDirective(ParentService, ParentDirective)],\r\n * })\r\n * export class ParentDirective {...}\r\n * ```\r\n * child.directive.ts:\r\n * ```\r\n * @Directive({\r\n * selector: 'child',\r\n * })\r\n * export class ChildDirective {\r\n * #parent = inject(ParentService);\r\n * }\r\n * ```\r\n *\r\n * @param token - The injection token to provide.\r\n * @param directive - The directive/component class that will be provided.\r\n * @returns A provider object that can be used in the providers array of an Angular module or component.\r\n */\r\nexport function provideServiceDirective<T extends Type<unknown>>(\r\n token: InjectionToken<T>,\r\n directive: T\r\n): Provider {\r\n return {\r\n provide: token,\r\n useExisting: forwardRef(() => directive)\r\n }\r\n}\r\n","/**\r\n * Angular Event Manager Plugin that enables event modifier syntax in templates.\r\n *\r\n * @example\r\n * ```html\r\n * <form (submit.pd)=\"onSubmit()\">...</form>\r\n * <div (click.sp)=\"handleClick()\">...</div>\r\n * <button (click.pd.sp)=\"handleButtonClick()\">Click me</button>\r\n * ```\r\n */\r\n\r\nimport { DOCUMENT } from '@angular/common';\r\nimport { Inject, Injectable, Provider } from '@angular/core';\r\nimport { EVENT_MANAGER_PLUGINS, EventManagerPlugin } from '@angular/platform-browser';\r\n\r\n/**\r\n * Event Manager Plugin that adds support for event modifiers.\r\n * @internal\r\n */\r\n@Injectable()\r\nexport class EventModifiersPlugin extends EventManagerPlugin {\r\n /**\r\n * Supported modifiers: `pd` (preventDefault), `sp` (stopPropagation)\r\n */\r\n private static modifierMap: { [key: string]: (e: Event) => void } = {\r\n 'pd': (e: Event) => e.preventDefault(),\r\n 'sp': (e: Event) => e.stopPropagation(),\r\n };\r\n\r\n constructor(@Inject(DOCUMENT) doc: Document) {\r\n super(doc);\r\n }\r\n\r\n supports(eventName: string): boolean {\r\n return eventName.includes('.') &&\r\n eventName.split('.').some(part => part in EventModifiersPlugin.modifierMap);\r\n }\r\n\r\n addEventListener(element: HTMLElement, eventName: string, handler: (event: Event) => void): () => void {\r\n const parts = eventName.split('.');\r\n const domEventName = parts[0];\r\n const modifiers = parts.slice(1).filter(m => m in EventModifiersPlugin.modifierMap);\r\n\r\n const wrappedHandler = (event: Event) => {\r\n modifiers.forEach(mod => EventModifiersPlugin.modifierMap[mod](event));\r\n\r\n return handler(event);\r\n };\r\n\r\n element.addEventListener(domEventName, wrappedHandler);\r\n\r\n return () => element.removeEventListener(domEventName, wrappedHandler);\r\n }\r\n}\r\n\r\n/**\r\n * Provides the Event Modifiers Plugin.\r\n *\r\n * @example\r\n * ```typescript\r\n * bootstrapApplication(AppComponent, {\r\n * providers: [provideEventModifiersPlugin()]\r\n * });\r\n * ```\r\n */\r\nexport function provideEventModifiersPlugin(): Provider {\r\n return {\r\n provide: EVENT_MANAGER_PLUGINS,\r\n useClass: EventModifiersPlugin,\r\n multi: true\r\n };\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCG;AACG,SAAU,uBAAuB,CACrC,KAAwB,EACxB,SAAY,EAAA;IAEZ,OAAO;AACL,QAAA,OAAO,EAAE,KAAK;AACd,QAAA,WAAW,EAAE,UAAU,CAAC,MAAM,SAAS;KACxC;AACH;;ACnDA;;;;;;;;;AASG;AAMH;;;AAGG;AAEG,MAAO,oBAAqB,SAAQ,kBAAkB,CAAA;AAC1D;;AAEG;IACK,OAAO,WAAW,GAA0C;QAClE,IAAI,EAAE,CAAC,CAAQ,KAAK,CAAC,CAAC,cAAc,EAAE;QACtC,IAAI,EAAE,CAAC,CAAQ,KAAK,CAAC,CAAC,eAAe,EAAE;KACxC;AAED,IAAA,WAAA,CAA8B,GAAa,EAAA;QACzC,KAAK,CAAC,GAAG,CAAC;IACZ;AAEA,IAAA,QAAQ,CAAC,SAAiB,EAAA;AACxB,QAAA,OAAO,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC;AAC5B,YAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,IAAI,oBAAoB,CAAC,WAAW,CAAC;IAC/E;AAEA,IAAA,gBAAgB,CAAC,OAAoB,EAAE,SAAiB,EAAE,OAA+B,EAAA;QACvF,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC;AAClC,QAAA,MAAM,YAAY,GAAG,KAAK,CAAC,CAAC,CAAC;QAC7B,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,oBAAoB,CAAC,WAAW,CAAC;AAEnF,QAAA,MAAM,cAAc,GAAG,CAAC,KAAY,KAAI;AACtC,YAAA,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI,oBAAoB,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;AAEtE,YAAA,OAAO,OAAO,CAAC,KAAK,CAAC;AACvB,QAAA,CAAC;AAED,QAAA,OAAO,CAAC,gBAAgB,CAAC,YAAY,EAAE,cAAc,CAAC;QAEtD,OAAO,MAAM,OAAO,CAAC,mBAAmB,CAAC,YAAY,EAAE,cAAc,CAAC;IACxE;AAhCW,IAAA,OAAA,IAAA,GAAA,EAAA,CAAA,kBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,oBAAoB,kBASX,QAAQ,EAAA,CAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;2GATjB,oBAAoB,EAAA,CAAA;;2FAApB,oBAAoB,EAAA,UAAA,EAAA,CAAA;kBADhC;;0BAUc,MAAM;2BAAC,QAAQ;;AA0B9B;;;;;;;;;AASG;SACa,2BAA2B,GAAA;IACzC,OAAO;AACL,QAAA,OAAO,EAAE,qBAAqB;AAC9B,QAAA,QAAQ,EAAE,oBAAoB;AAC9B,QAAA,KAAK,EAAE;KACR;AACH;;ACvEA;;AAEG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import * as i0 from '@angular/core';
|
|
2
1
|
import { Type, InjectionToken, Provider } from '@angular/core';
|
|
3
2
|
|
|
4
|
-
declare class UtilsModule {
|
|
5
|
-
static ɵfac: i0.ɵɵFactoryDeclaration<UtilsModule, never>;
|
|
6
|
-
static ɵmod: i0.ɵɵNgModuleDeclaration<UtilsModule, never, never, never>;
|
|
7
|
-
static ɵinj: i0.ɵɵInjectorDeclaration<UtilsModule>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
3
|
/**
|
|
11
4
|
* Utility function to create a provider for a directive/component that can be injected as a service.
|
|
12
5
|
* This is useful for cases where you want to inject a directive/component instance into another directive/component.
|
|
@@ -50,4 +43,16 @@ declare class UtilsModule {
|
|
|
50
43
|
*/
|
|
51
44
|
declare function provideServiceDirective<T extends Type<unknown>>(token: InjectionToken<T>, directive: T): Provider;
|
|
52
45
|
|
|
53
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Provides the Event Modifiers Plugin.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* bootstrapApplication(AppComponent, {
|
|
52
|
+
* providers: [provideEventModifiersPlugin()]
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function provideEventModifiersPlugin(): Provider;
|
|
57
|
+
|
|
58
|
+
export { provideEventModifiersPlugin, provideServiceDirective };
|
package/utils/README.md
CHANGED
|
@@ -1,3 +1,128 @@
|
|
|
1
1
|
# @telperion/ng-pack/utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Angular utility functions and plugins for enhanced development experience
|
|
4
|
+
|
|
5
|
+
## Motivation
|
|
6
|
+
|
|
7
|
+
Provide reusable utilities and plugins for Angular applications to reduce boilerplate and enhance developer productivity.
|
|
8
|
+
|
|
9
|
+
## Goals
|
|
10
|
+
|
|
11
|
+
* TypeScript support
|
|
12
|
+
* Tree-shaking
|
|
13
|
+
* Minimal dependencies
|
|
14
|
+
* Type-safe
|
|
15
|
+
* Well documented
|
|
16
|
+
* High test coverage
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @telperion/ng-pack
|
|
22
|
+
# or
|
|
23
|
+
yarn add @telperion/ng-pack
|
|
24
|
+
# or
|
|
25
|
+
pnpm add @telperion/ng-pack
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
### Event Modifiers Plugin
|
|
31
|
+
|
|
32
|
+
Angular Event Manager Plugin that enables event modifier syntax in templates, similar to Vue.js.
|
|
33
|
+
|
|
34
|
+
#### Usage
|
|
35
|
+
|
|
36
|
+
First, provide the plugin in your application:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { provideEventModifiersPlugin } from '@telperion/ng-pack/utils';
|
|
40
|
+
|
|
41
|
+
bootstrapApplication(AppComponent, {
|
|
42
|
+
providers: [provideEventModifiersPlugin()]
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Then use modifiers in your templates:
|
|
47
|
+
|
|
48
|
+
```html
|
|
49
|
+
<!-- Prevent default form submission -->
|
|
50
|
+
<form (submit.pd)="onSubmit()">...</form>
|
|
51
|
+
|
|
52
|
+
<!-- Stop event propagation -->
|
|
53
|
+
<div (click.sp)="handleClick()">...</div>
|
|
54
|
+
|
|
55
|
+
<!-- Chain multiple modifiers -->
|
|
56
|
+
<button (click.pd.sp)="handleButtonClick()">Click me</button>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### Available Modifiers
|
|
60
|
+
|
|
61
|
+
- `pd` - Calls `preventDefault()` on the event
|
|
62
|
+
- `sp` - Calls `stopPropagation()` on the event
|
|
63
|
+
|
|
64
|
+
### Provide Service Directive
|
|
65
|
+
|
|
66
|
+
Utility function to create a provider for a directive/component that can be injected as a service. Useful for parent-child directive communication.
|
|
67
|
+
|
|
68
|
+
#### Usage
|
|
69
|
+
|
|
70
|
+
**1. Create an injection token:**
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// parent.service.ts
|
|
74
|
+
import { InjectionToken } from "@angular/core";
|
|
75
|
+
import type { ParentDirective } from "./parent.directive";
|
|
76
|
+
|
|
77
|
+
export const ParentService = new InjectionToken<ParentDirective>("ParentService");
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**2. Provide the directive as a service:**
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// parent.directive.ts
|
|
84
|
+
import { Directive } from "@angular/core";
|
|
85
|
+
import { provideServiceDirective } from "@telperion/ng-pack/utils";
|
|
86
|
+
import { ParentService } from "./parent.service";
|
|
87
|
+
|
|
88
|
+
@Directive({
|
|
89
|
+
selector: '[parent]',
|
|
90
|
+
providers: [provideServiceDirective(ParentService, ParentDirective)],
|
|
91
|
+
})
|
|
92
|
+
export class ParentDirective {
|
|
93
|
+
doSomething() {
|
|
94
|
+
console.log('Parent action');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**3. Inject in child directive:**
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// child.directive.ts
|
|
103
|
+
import { Directive, inject } from "@angular/core";
|
|
104
|
+
import { ParentService } from "./parent.service";
|
|
105
|
+
|
|
106
|
+
@Directive({
|
|
107
|
+
selector: '[child]',
|
|
108
|
+
})
|
|
109
|
+
export class ChildDirective {
|
|
110
|
+
private parent = inject(ParentService);
|
|
111
|
+
|
|
112
|
+
ngOnInit() {
|
|
113
|
+
this.parent.doSomething();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**4. Use in template:**
|
|
119
|
+
|
|
120
|
+
```html
|
|
121
|
+
<div parent>
|
|
122
|
+
<div child></div>
|
|
123
|
+
</div>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|