- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1
 
Docs: Utils
- Introduction
 - Installation
 - Overview
 - Core Module
 - Key Components
 - Directives
 - Pipes
 - Services
 - Tokens
 - Data Services
 - Usage Examples
 - Testing
 
The Angular Utils Library provides utility components, directives, pipes, services, and tokens to simplify application development. This shared library contains reusable elements that can be used across multiple projects, offering functionality like dynamic content rendering, numeral formatting, custom structural directives, data services, and module loaders.
Install the library and its dependencies using npm:
npm install @your-org/utilsThe UtilsModule is the main module exported by this library. It aggregates all utilities into a single module that can be imported into any Angular application.
- 
Dynamic Content Rendering via 
DynamicContentViewerComponent. - 
Custom Structural Directives like 
ForDirective. - 
Pipes for numeral formatting and handling empty values (
NotAvailablePipe,NumeralPipe). - 
Runtime Module Loading via 
ModuleLoaderService. - 
Style Loading for dynamically applying themes via 
StyleLoaderService. - Noop Data Service for mock data handling.
 - Async API Call Helpers for handling promises as observables.
 
The UtilsModule is the primary entry point for the library. It includes declarations and exports for all components, directives, pipes, and other utilities.
@NgModule({
  imports: [CommonModule],
  declarations: [
    NumeralPipe,
    NotAvailablePipe,
    DynamicContentViewer,
    ForDirective,
  ],
  exports: [
    NumeralPipe,
    NotAvailablePipe,
    DynamicContentViewer,
    ForDirective,
  ],
})
export class UtilsModule {}To use the module, import it into your application:
import { UtilsModule } from '@your-org/utils';
@NgModule({
  imports: [UtilsModule],
})
export class AppModule {}The DynamicContentViewer is designed to render dynamic content at runtime. It parses HTML content and dynamically instantiates Angular components embedded within.
- Dynamic Component Rendering: Automatically matches selectors for embedded components.
 - Event Output: Emits an event when the content is fully rendered.
 
- 
content: Raw HTML content with embedded Angular components. - 
data: Data passed to the component. 
- 
docRendered: Emits when the content has been successfully rendered. 
<classifieds-ui-dynamic-content-viewer
  [content]="htmlContent"
  [data]="contentData"
  (docRendered)="onContentRendered()"
></classifieds-ui-dynamic-content-viewer>The ForDirective is a custom structural directive that creates elements in a loop. It also provides convenient context variables (even, odd, first, and last) for use within templates.
<div *for="5">
  Index: {{ index }}, Even: {{ even }}, First: {{ first }}, Last: {{ last }}
</div>This will render the block five times, with the context variables dynamically updated for each instance.
Transforms numbers using formatting rules.
- Converts string-based numbers into 
Numberobjects. - Supports numeral transformations (basic number formatting).
 
<span>{{ value | numeral: '0,0.00' }}</span>Provides a default fallback for empty or invalid values.
<span>{{ value | na: 'Default Value' }}</span>If value is null or empty, 'Default Value' is displayed.
Dynamically loads Angular modules at runtime. Useful for lazy loading modules or features dynamically.
constructor(private moduleLoader: ModuleLoaderService) {}
loadModule() {
  this.moduleLoader.loadModule(() => import('./lazy/lazy.module'));
}Dynamically loads and applies stylesheet themes by updating the <link> element.
constructor(private styleLoader: StyleLoaderService) {}
applyTheme(theme: string) {
  this.styleLoader.loadStyle(`/assets/themes/${theme}.css`);
}Converts promises into observables, making asynchronous tasks more manageable and cancelable.
constructor(private asyncHelper: AsyncApiCallHelperService) {}
processTask() {
  this.asyncHelper.doTask(fetch('/api/data')).subscribe({
    next: (result) => console.log(result),
    error: (err) => console.error(err),
  });
}The library defines custom InjectionTokens for dependency injection configurations.
- 
EMBEDDABLE_COMPONENT: Injects dynamic embeddable components into 
DynamicContentViewerComponent. - SITE_NAME: String token for the site name.
 - HOST_NAME: String token for the host name.
 - PROTOCOL: String token for the request protocol.
 
providers: [
  { provide: SITE_NAME, useValue: 'MySite' },
  { provide: HOST_NAME, useValue: 'example.com' },
  { provide: PROTOCOL, useValue: 'https' },
]Provides a mock implementation of EntityCollectionDataService. This is useful for testing or temporary mock data setup.
- 
add(entity: T): Adds an entity. - 
delete(id: number | string): Deletes an entity by its ID. - 
getAll(): Retrieves all entities. - 
getById(id: any): Retrieves an entity by its ID. - Other CRUD operations (
getWithQuery,update,upsert). 
const service = new NoopDataService<any>('mock');
service.add({ id: 1, name: 'Test' }).subscribe((data) => console.log(data));<classifieds-ui-dynamic-content-viewer
  [content]="htmlContent"
  [data]="componentData"
></classifieds-ui-dynamic-content-viewer>htmlContent = `<custom-element></custom-element>`;
componentData = { key: 'value' };<div *for="10">Iteration {{ index }}</div>constructor(private asyncHelper: AsyncApiCallHelperService) {}
processApiCall() {
  this.asyncHelper.doTask(fetch('/api/items')).subscribe({
    next: (data) => console.log(data),
    error: (err) => console.error(err),
  });
}The library components, directives, pipes, and services can be tested using standard Angular testing utilities and Karma.
import { NumeralPipe } from './numeral.pipe';
describe('NumeralPipe', () => {
  it('should create an instance', () => {
    const pipe = new NumeralPipe();
    expect(pipe).toBeTruthy();
  });
});The Angular Utils Library provides reusable utility components, directives, pipes, and services to simplify application development. Incorporate this library wherever you need dynamic rendering, numeral handling, or runtime module and style management in your Angular applications.
For contributions or feature requests, feel free to contact the development team!