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
πŸ“„ Content Security Policy (CSP) Interceptor

#angular #interceptor

A CSP interceptor can be used to automatically add Content Security Policy headers to outgoing HTTP requests to improve security.


import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';

@Injectable()
export class CspInterceptor implements HttpInterceptor {

intercept(
request: HttpRequest<any>,
next: HttpHandler
) {
const cspHeader = "default-src 'self'; script-src 'self' 'unsafe-inline'";
const cspRequest = request.clone({
setHeaders: {
'Content-Security-Policy': cspHeader,
},
});
return next.handle(cspRequest);
}
}
πŸ“„ Angular Custom Directives: Confirm Dialog Directive

#angular #directive

Creating a custom directive in Angular to implement a confirm dialog can help you standardize and simplify the process of confirming actions before executing them, such as deleting items or making irreversible changes. In this example, we’ll create a custom directive named appConfirmDialog to open a confirmation dialog before executing an action.


<button 
[appConfirmDialog]="'Are you sure you want to perform this action?'"
>Delete</button>
πŸ“„ Angular Interceptors: Compression Interceptor

#angular #interceptor

A compression interceptor can be used to automatically request compressed content (e.g., gzip) from the server, reducing the amount of data transferred over the network.


import { Injectable } from '@angular/core';
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
} from '@angular/common/http';

@Injectable()
export class CompressionInterceptor implements HttpInterceptor {
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
) {
const compressedRequest = request.clone({
setHeaders: {
'Accept-Encoding': 'gzip, deflate'
},
});
return next.handle(compressedRequest);
}
}
πŸ“„ Angular Custom Directives: Infinite Scroll Directive

#angular #directive

Creating a custom directive in Angular for implementing infinite scrolling can significantly improve the user experience, especially when dealing with long lists of data. In this example, we’ll create a custom directive named appInfiniteScroll to load additional content as the user scrolls down a page.


<div
appInfiniteScroll
class="scrollable-content"
(scrolled)="loadMoreData()"
>
<!-- Your list of items -->
</div>


import { Directive, ElementRef, HostListener, Input, Output, EventEmitter } from '@angular/core';

@Directive({
selector: '[appInfiniteScroll]'
})
export class InfiniteScrollDirective {
@Input() scrollThreshold = 100;
@Output() scrolled = new EventEmitter<void>();

constructor(private el: ElementRef) {}

@HostListener('scroll', ['$event'])
onScroll(event: Event): void {
const {
scrollHeight,
scrollTop,
clientHeight
} = event.target as HTMLElement;
const atBottom = scrollHeight - scrollTop <= clientHeight + this.scrollThreshold;

if (atBottom) {
this.scrolled.emit();
}
}
}
πŸ‘1
Hello, friends!
As you can see, I'm a Ukrainian software engineer who tries to do volunteer fundraisers to support the Armed Forces of Ukraine. Sometimes it takes a lot of time and I am not able to maintain this channel for you. Please help me close these fundraisers because without the Ukrainian army there will be nothing Ukrainian. Thank you very much!

πŸ’΅ FOR Ukraine:
https://send.monobank.ua/jar/697qLyfKgT
5375 4114 1222 8582

🌎 FOR ALL DONATS:

πŸ’΅ SWIFT code: UNJSUAUKXXX

πŸ’΅ PayPal: luckystudydanit@gmail.com

My profile with reports after closing fundraiser :
https://www.facebook.com/volunt2erua/

also all reports in our πŸš€ channel:
https://t.me/toxicc_squad
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘6
πŸ€“ Memory Leaks in JavaScript

#js

In JavaScript, a memory leak occurs when a program reserves memory for objects or data that are no longer needed or referenced, preventing the JavaScript engine’s garbage collector from freeing up that memory. Over time, this can lead to performance issues, such as sluggishness and unresponsiveness in web applications.


βœ… Article link: https://medium.com/@stheodorejohn/memory-leaks-in-javascript-causes-solutions-and-best-practices-18d8faecc672
πŸ‘2
πŸ“„ Angular Custom Directives: Highlight Search Results Directive

#angular #directive

Creating a custom directive in Angular to highlight search results within a block of text can improve the user experience when searching for specific terms in your application. In this example, we’ll create a custom directive named `appHighlightSearch` to highlight search results in a text block.


<p [appHighlightSearch]="searchQuery">
Lorem ipsum dolor sit amet, ...
</p>
πŸ‘2πŸ‘Ž1
πŸ“„ Angular Custom Directives: Responsive Directive

#angular #directive

Creating a custom directive in Angular to control the visibility of elements based on the screen size can help you create responsive designs. In this example, we’ll create a custom directive named appResponsive to show or hide elements based on the screen size.


<div [appResponsive]="'md, lg'">
This content is visible on medium and large screens.
</div>