Angular πŸ‡ΊπŸ‡¦ - practical notes
1.63K subscribers
1.6K photos
1 file
532 links
Angular - practical notes

This group is for posting practical notes for Angular developers. Mostly all posts are for quick implementation https://t.me/angular_practical_notes (Commenting on posts only in ENG and UA langs here). Welcome!
Download Telegram
πŸ“„ Angular Custom Directives: Disable Right-Click Directive

#angular #directive

Creating a custom directive in Angular to disable right-clicking can be useful in scenarios where you want to prevent users from accessing context menus or taking specific actions using the right mouse button. In this example, we’ll create a custom directive named `appDisableRightClick` to prevent right-clicking on elements.


import { Directive, HostListener } from '@angular/core';

@Directive({
selector: '[appDisableRightClick]'
})
export class DisableRightClickDirective {
constructor() {}

@HostListener('contextmenu', ['$event'])
onRightClick(event: Event): void {
event.preventDefault();
}
}


<div appDisableRightClick>
Right-clicking is disabled on this element.
</div>
πŸ‘4
πŸ“„ Angular Custom Directives: TimeAgo Directive

#angular #directive

Creating a custom directive in Angular to display a time ago (relative time) can be useful for showing how long ago an event occurred. In this example, we’ll create a custom directive named appTimeAgo to display the time difference in a user-friendly format.


<p [appTimeAgo]="postDate"></p>
πŸ‘Ž1
πŸ“„ Tracking an inactive user using RXJS

#angular #rxjs

User inactivity can be defined as a period during which a user does not interact with the application. Tracking this inactivity helps in various scenarios, such as optimizing resource usage, enhancing security, and providing a better user experience.


// inactivity.service.ts

import { Injectable } from '@angular/core';
import { Observable, fromEvent, timer } from 'rxjs';
import { mergeMap, mapTo } from 'rxjs/operators';

@Injectable({ providedIn: 'root'})
export class InactivityService {

public trackInactivity(
duration: number
): Observable<boolean> {
const mouseMove$ = fromEvent(document,'mousemove')
.pipe(mapTo(true));

const keyDown$ = fromEvent(document,'keydown')
.pipe(mapTo(true));

return timer(0, duration)
.pipe(
mergeMap(() => mouseMove$ || keyDown$)
);
}
}



βœ… Article link: https://designtechworld.medium.com/angular-tracking-an-inactive-user-using-rxjs-in-built-and-custom-events-33d2e95fd167
πŸ”₯1
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
❀2
πŸ“„ Angular Internationalization Set-up in Existing Project

#angular #internationalization #i18n

Step 1: Install @angular/localize

$ ng add @angular/localize


Step 2: Update Angular JSON

Second screen

Step 3: Extract Messages

$ ng extract-i18n --output-path src/locale


This command will create XLF files in the specified locations (src/locale/messages.ar.xlf,src/locale/messages.fr.xlf)


Step 4: Translate Messages

Translate the messages in each XLF file for the supported languages as mentioned in Angular Documentation here.


Step 5: Build and Serve

$ ng build --localize
$ ng serve --configuration fr --port 4100 // Opens french version on port 4100
$ ng serve --configuration ar --port 4200 // Opens arabic version on port 4200


βœ… Article link: https://levelup.gitconnected.com/angular-internationalization-set-up-in-existing-project-891dcf6cee95
❀2
πŸ“„ Master JavaScript Generators: 5 Practical Use Cases

#js #generators

Generators are these magical functions that you pause and resume, whenever you want β€” they don’t execute continuously.


βœ… Article link: https://javascript.plainenglish.io/javascript-generators-e4cdaa02839a
πŸ“„ "Goodbye NgRx Facades. Hello Standalone Functions"

#angular #ngrx

Let’s list out the benefit of standalone functions:

-- Decouple tight dependency between the component and the store.
-- Simplifies CRUD operation with the store, by providing simple interfaces for the component to use to interact with the store.
-- Reusability!
-- No need to create a service(facade) to encapsulate NgRx logic.
-- Stand alone functions are not tied to a class, meaning each can be imported and used individually.
-- Reduces the need to inject multiple facade dependencies.


βœ… Article link: https://javascript.plainenglish.io/goodbye-ngrx-facades-hello-standalone-functions-7b7606c01659
πŸ”₯2
πŸ€“ REST API Design Best Practices

#api #restApi

There are different types of API protocols:

REST β€” relies on a client/server approach that separates the front and back ends of the API and provides considerable flexibility in development and implementation.
RPC β€” The remote procedural call (RPC) protocol sends multiple parameters and receives results.
SOAP β€” Supports a wide range of communication protocols found across the internet, such as HTTP, SMTP, and TCP.
WebSocket β€” Provides a way to exchange data between browser and server via a persistent connection.


βœ… Article link: https://medium.com/@techworldwithmilan/rest-api-design-best-practices-2eb5e749d428
❀2