Generate components in angular, usefull commands
ng g component <name>
ng g c
ng g directive <name>
ng g d
ng g guard <name>
ng g g
ng g interceptor <name>
ng g module <name>
ng g m
ng g pipe <name>
ng g p
ng g service <name>
ng g s
List of some commonly used Angular pipes
Angular provides a variety of built-in pipes that you can use to transform and format data in your templates. Here’s a list of some commonly used Angular pipes, along with examples of how to use them:
- DatePipe: Formats a date object into a string representation based on the provided format.
<!-- Example: Displaying the current date in a specific format -->
{{ today | date: 'dd/MM/yyyy' }}
- UpperCasePipe: Converts a string to all uppercase letters.
<!-- Example: Converting a string to uppercase -->
{{ 'Hello, Angular' | uppercase }}
- LowerCasePipe: Converts a string to all lowercase letters.
<!-- Example: Converting a string to lowercase -->
{{ 'Hello, Angular' | lowercase }}
- CurrencyPipe: Formats a number as currency, with options for specifying the currency code and formatting.
<!-- Example: Displaying a number as currency -->
{{ price | currency: 'USD':true }}
- DecimalPipe: Formats a number as a decimal with options for specifying the number of digits and decimal places.
<!-- Example: Displaying a number with 2 decimal places -->
{{ value | number: '1.2-2' }}
- PercentPipe: Formats a number as a percentage.
<!-- Example: Displaying a number as a percentage -->
{{ fraction | percent }}
- JsonPipe: Converts a JavaScript object into a JSON string.
<!-- Example: Displaying a JSON object as a string -->
{{ data | json }}
- AsyncPipe: Handles observables and promises and automatically subscribes/unsubscribes in the template.
<!-- Example: Displaying the value of an observable -->
{{ observableData | async }}
- SlicePipe: Extracts a portion of an array or string.
<!-- Example: Displaying the first 3 items of an array -->
{{ arrayData | slice:0:3 }}
- TitleCasePipe: Converts a string to title case (the first letter of each word is capitalized).
<!-- Example: Converting a string to title case --> {{ 'hello world' | titlecase }}
- I18nPluralPipe: Selects an output string according to the pluralization rules for a given locale.
<!-- Example: Selecting a pluralized string based on a count --> {{ itemCount | i18nPlural: { '=0': 'No items', '=1': 'One item', 'other': '# items' } }}
- I18nSelectPipe: Selects an output string based on a mapping of input values to output strings for a given locale.
<!-- Example: Selecting an output string based on an input value --> {{ status | i18nSelect: { 'in-progress': 'Work in progress', 'completed': 'Task completed', 'pending': 'Task pending' } }}
Custom pipes can also be created if specific data transformation needs that are not covered by the built-in pipes.
Custom pipes allows to encapsulate and reuse data formatting logic in your Angular application.
List of Angular directives
Angular provides a variety of built-in directives that you can use to extend HTML and add dynamic behavior to your templates. Here’s a list of some commonly used Angular directives with examples:
- ngIf: Conditionally renders an element based on a boolean expression.
<div *ngIf="isUserLoggedIn">Welcome, User!</div>
- ngFor: Iterates over a collection and generates HTML for each item.
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
- ngSwitch: Conditionally renders content based on matching cases.
<div [ngSwitch]="userRole">
<div *ngSwitchCase="'admin'">Admin Dashboard</div>
<div *ngSwitchCase="'user'">User Dashboard</div>
<div *ngSwitchDefault>Guest Dashboard</div>
</div>
- ngClass: Adds or removes CSS classes based on conditions.
<div [ngClass]="{ 'active': isActive, 'disabled': isDisabled }">Clickable Element</div>
- ngStyle: Sets inline styles based on expressions.
<div [ngStyle]="{ 'font-size.px': fontSize, 'color': fontColor }">Styled Text</div>
- ngModel: Two-way data binding for form elements.
<input [(ngModel)]="userName" placeholder="Enter your name">
- ngIfElse: Provides an alternative template when ngIf condition is false.
<div *ngIf="isUserLoggedIn; else notLoggedIn">Welcome, User!</div>
<ng-template #notLoggedIn>Please log in to access the content.</ng-template>
- ngTemplateOutlet: Renders the content of a named template.
<ng-container *ngTemplateOutlet="myTemplate"></ng-container>
<ng-template #myTemplate><p>This is a custom template.</p></ng-template>
- ng-container: A structural directive used for grouping elements without introducing an extra HTML element.
<ng-container *ngFor="let item of items">
<div>{{ item }}</div>
</ng-container>
- ngDisable: Disables or enables an element based on a condition.
<button [disabled]="isButtonDisabled">Click me</button>
- ngSubmit: Used in forms to handle form submission.
<form (ngSubmit)="onFormSubmit()"> <!-- Form inputs go here --> <button type="submit">Submit</button> </form>
Angular also allows to create custom directives to extend HTML behavior.