Skip to content

Instantly share code, notes, and snippets.

View rakia's full-sized avatar

Rakia Ben Sassi rakia

View GitHub Profile
@rakia
rakia / redirect-to.ts
Created May 23, 2024 07:57
Angular Routing: redirectTo as function
const routes: Routes = [
{ path: "dashboard", component: DashboardComponent },
{
path: "profiles",
redirectTo: ({ queryParams }) => {
const errorHandler = inject(ErrorHandler);
const userId = queryParams['userId'];
if (userId) {
// return the appropriate redirect path based on the userId query-parameter
return `/profiles/${userId}`;
@rakia
rakia / per-route-renderMode.ts
Created May 22, 2024 21:01
Angular routing: per-route renderMode
const routes: Routes = [
{
path: 'route1',
component: Route1Component,
renderMode: 'stategy1' // renderMode option 1
},
{
path: 'route2',
component: Route2Component,
renderMode: 'strategy2' // renderMode option 2
@rakia
rakia / redirect-command.ts
Created May 22, 2024 20:47
Angular Routing: RedirectCommand
const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [
() => {
const router: Router = inject(Router);
const urlTree: UrlTree = router.parseUrl('/error');
return new RedirectCommand(urlTree, { skipLocationChange: true });
},
@rakia
rakia / control-state-change-events.ts
Created May 22, 2024 20:25
Angular: Unified Control State Change Events
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class ProfileFormComponent {
private fb = inject(FormBuilder);
profileForm: FormGroup;
ngOnInit() {
this.profileForm = this.fb.group({
firstName: ['', [Validators.required, Validators.minLength(2)]],
@rakia
rakia / default-content.ts
Created May 22, 2024 20:12
Angular: default content
@Component({
selector: 'main-layout',
template: `
<ng-content select="header">Hello again</ng-content>
<ng-content><p>Default content if none is provided.</p></ng-content>
<ng-content select="footer"></ng-content>
`
})
export class MainLayoutComponent {}
@rakia
rakia / model-input.ts
Created May 22, 2024 19:59
Angular: Model input example
import { Component, model, input } from '@angular/core';
@Component({
selector: 'app-user',
template: '<button (click)="upgradeSubscription()">Upgrade Subscription</button>',
})
export class UserComponent {
// a standard input, it's read-only
isSubscribed = input(true);
@rakia
rakia / zoneless-component.ts
Created May 22, 2024 19:51
Angular: zoneless component
import { bootstrapApplication } from '@angular/platform-browser';
import { provideExperimentalZonelessChangeDetection } from '@angular/core';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, {
providers: [
provideExperimentalZonelessChangeDetection(),
],
});
@rakia
rakia / control-flow.html
Created May 22, 2024 18:03
Angular: Control Flow example
@if (isOrganization) {
<profile-organization [data]="userData"></profile-organization>
} @else if (isPremium) {
<profile-premium [data]="userData"></profile-premium>
} @else {
<profile-basic [data]="userData"></profile-basic>
}
@rakia
rakia / memory-safe-python-program.py
Created May 3, 2024 17:17
A memory-safe Python program
# Python handles memory management for you
list_of_items = ['item1', 'item2', 'item3']
print("Current list:", list_of_items)
# When objects like lists are no longer needed, Python cleans up
del list_of_items # This frees up memory space automatically
# The garbage collector in Python manages the allocation and deallocation of memory
@rakia
rakia / remote-control.c
Created May 3, 2024 16:51
A C program with remote control vulnerability
#include <stdio.h>
#include <string.h>
void process_command(char *input) {
char buffer[30];
strcpy(buffer, input); // Potential overflow here
// ... Process command
}
int main() {
char command[100];