Angular vs Solid.js Comparison
Reactivity
Declare state
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-name",
template: `<h1>Hello {{ name }}</h1>`,
})
export class NameComponent {
name = "John";
}
@NgModule({
declarations: [NameComponent],
exports: [NameComponent],
})
export class NameModule {}
Solid.js
import { createSignal } from "solid-js";
export default function Name() {
const [name] = createSignal("John");
return <h1>Hello {name()}</h1>;
}
Update state
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-name",
template: `<h1>Hello {{ name }}</h1>`,
})
export class NameComponent {
name = "John";
constructor() {
this.name = "Jane";
}
}
@NgModule({
declarations: [NameComponent],
exports: [NameComponent],
})
export class NameModule {}
Solid.js
import { createSignal } from "solid-js";
export default function Name() {
const [name, setName] = createSignal("John");
setName("Jane");
return <h1>Hello {name()}</h1>;
}
Computed state
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-doublecount",
template: `<div>{{ doubleCount }}</div>`,
})
export class DoublecountComponent {
count = 10;
get doubleCount() {
return this.count * 2;
}
}
@NgModule({
declarations: [DoublecountComponent],
exports: [DoublecountComponent],
})
export class DoublecountModule {}
Solid.js
import { createSignal } from "solid-js";
export default function DoubleCount() {
const [count] = createSignal(10);
const doubleCount = () => count() * 2;
return <div>{doubleCount()}</div>;
}
Templating
Minimal template
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-helloworld",
template: `<h1>Hello world</h1>`,
})
export class HelloworldComponent {}
@NgModule({
declarations: [HelloworldComponent],
exports: [HelloworldComponent],
})
export class HelloworldModule {}
Solid.js
export default function HelloWorld() {
return <h1>Hello World!</h1>;
}
Styling
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-cssstyle",
template: `
<h1 class="title">I am red</h1>
<button style="font-size: 10rem">I am a button</button>
`,
styles: [
`
.title {
color: red;
}
`,
],
})
export class CssStyleComponent {}
@NgModule({
declarations: [CssStyleComponent],
exports: [CssStyleComponent],
})
export class CssStyleModule {}
Solid.js
import "./style.css";
export default function CssStyle() {
return (
<>
<h1 class="title">I am red</h1>
<button style={{ "font-size": "10rem" }}>I am a button</button>
</>
);
}
Loop
Angular
import { Component, NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
@Component({
selector: "app-colors",
template: `
<ul>
<li *ngFor="let color of colors">{{ color }}</li>
</ul>
`,
})
export class ColorsComponent {
colors = ["red", "green", "blue"];
}
@NgModule({
declarations: [ColorsComponent],
imports: [CommonModule],
exports: [ColorsComponent],
})
export class ColorsModule {}
Solid.js
import { For } from "solid-js";
export default function Colors() {
const colors = ["red", "green", "blue"];
return (
<ul>
<For each={colors}>{(color) => <li>{color}</li>}</For>
</ul>
);
}
Event click
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-counter",
template: `
<p>Counter: {{ count }}</p>
<button (click)="incrementCount()">+1</button>
`,
})
export class CounterComponent {
count = 0;
incrementCount() {
this.count++;
}
}
@NgModule({
declarations: [CounterComponent],
exports: [CounterComponent],
})
export class CounterModule {}
Solid.js
import { createSignal } from "solid-js";
export default function Counter() {
const [count, setCount] = createSignal(0);
function incrementCount() {
setCount(count() + 1);
}
return (
<>
<p>Counter: {count()}</p>
<button onClick={incrementCount}>+1</button>
</>
);
}
Dom ref
Angular
import {
Component,
ViewChild,
ElementRef,
OnInit,
NgModule,
} from "@angular/core";
@Component({
selector: "app-inputfocused",
template: `<input type="text" #inputRef />`,
})
export class InputfocusedComponent implements OnInit {
@ViewChild("inputRef", { static: true })
inputRef!: ElementRef<HTMLInputElement>;
ngOnInit() {
this.inputRef.nativeElement.focus();
}
}
@NgModule({
declarations: [InputfocusedComponent],
exports: [InputfocusedComponent],
})
export class InputfocusedModule {}
Solid.js
import { onMount } from "solid-js";
export default function InputFocused() {
let inputElement;
onMount(() => inputElement.focus());
return <input ref={inputElement} type="text" />;
}
Conditional
Angular
import { Component, NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
const TRAFFIC_LIGHTS = ["red", "orange", "green"];
@Component({
selector: "app-trafficlight",
template: `
<button (click)="nextLight()">Next light</button>
<p>Light is: {{ light }}</p>
<p>
You must
<ng-container [ngSwitch]="light">
<span *ngSwitchCase="'red'">STOP</span>
<span *ngSwitchCase="'orange'">SLOW DOWN</span>
<span *ngSwitchCase="'green'">GO</span>
</ng-container>
</p>
`,
})
export class TrafficlightComponent {
lightIndex = 0;
get light() {
return TRAFFIC_LIGHTS[this.lightIndex];
}
nextLight() {
this.lightIndex = (this.lightIndex + 1) % TRAFFIC_LIGHTS.length;
}
}
@NgModule({
declarations: [TrafficlightComponent],
imports: [CommonModule],
exports: [TrafficlightComponent],
})
export class TrafficlightModule {}
Solid.js
import { createSignal, Switch, Match } from "solid-js";
const TRAFFIC_LIGHTS = ["red", "orange", "green"];
export default function TrafficLight() {
const [lightIndex, setLightIndex] = createSignal(0);
const light = () => TRAFFIC_LIGHTS[lightIndex()];
function nextLight() {
setLightIndex((lightIndex() + 1) % TRAFFIC_LIGHTS.length);
}
return (
<>
<button onClick={nextLight}>Next light</button>
<p>Light is: {light()}</p>
<p>
You must
<Switch>
<Match when={light() === "red"}>
<span>STOP</span>
</Match>
<Match when={light() === "orange"}>
<span>SLOW DOWN</span>
</Match>
<Match when={light() === "green"}>
<span>GO</span>
</Match>
</Switch>
</p>
</>
);
}
Lifecycle
On mount
Angular
import { Component, NgModule, OnInit } from "@angular/core";
@Component({
selector: "app-pagetitle",
template: `<p>Page title: {{ pageTitle }}</p>`,
})
export class PagetitleComponent implements OnInit {
pageTitle = "";
ngOnInit() {
this.pageTitle = document.title;
}
}
@NgModule({
declarations: [PagetitleComponent],
exports: [PagetitleComponent],
})
export class PagetitleModule {}
Solid.js
import { createSignal, onMount } from "solid-js";
export default function PageTitle() {
const [pageTitle, setPageTitle] = createSignal("");
onMount(() => {
setPageTitle(document.title);
});
return <p>Page title: {pageTitle()}</p>;
}
On unmount
Angular
import { Component, NgModule, OnDestroy } from "@angular/core";
@Component({
selector: "app-time",
template: `<p>Current time: {{ time }}</p>`,
})
export class TimeComponent implements OnDestroy {
time = new Date().toLocaleTimeString();
timer = setInterval(() => {
this.time = new Date().toLocaleTimeString();
}, 1000);
ngOnDestroy() {
clearInterval(this.timer);
}
}
@NgModule({
declarations: [TimeComponent],
exports: [TimeComponent],
})
export class TimeModule {}
Solid.js
import { createSignal, onCleanup } from "solid-js";
export default function Time() {
const [time, setTime] = createSignal(new Date().toLocaleTimeString());
const timer = setInterval(() => {
setTime(new Date().toLocaleTimeString());
}, 1000);
onCleanup(() => clearInterval(timer));
return <p>Current time: {time()}</p>;
}
Component composition
Props
Angular
import { Component, NgModule } from "@angular/core";
import { UserprofileModule } from "./userprofile.component";
@Component({
selector: "app-root",
template: `
<app-userprofile
name="John"
[age]="20"
[favouriteColors]="['green', 'blue', 'red']"
[isAvailable]="true"
>
</app-userprofile>
`,
})
export class AppComponent {}
@NgModule({
declarations: [AppComponent],
imports: [UserprofileModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Solid.js
import UserProfile from "./UserProfile.jsx";
export default function App() {
return (
<UserProfile
name="John"
age={20}
favouriteColors={["green", "blue", "red"]}
isAvailable
/>
);
}
Emit to parent
Angular
import { Component, NgModule } from "@angular/core";
import { AnswerButtonModule } from "./answer-button.component";
@Component({
selector: "app-root",
template: `
<p>Are you happy?</p>
<app-answer-button (yes)="onAnswerYes()" (no)="onAnswerNo()">
</app-answer-button>
<p style="font-size: 50px">{{ isHappy ? "😀" : "😥" }}</p>
`,
})
export class AppComponent {
isHappy = true;
onAnswerYes() {
this.isHappy = true;
}
onAnswerNo() {
this.isHappy = false;
}
}
@NgModule({
declarations: [AppComponent],
imports: [AnswerButtonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Solid.js
import { createSignal } from "solid-js";
import AnswerButton from "./AnswerButton.jsx";
export default function App() {
const [isHappy, setIsHappy] = createSignal(true);
function onAnswerNo() {
setIsHappy(false);
}
function onAnswerYes() {
setIsHappy(true);
}
return (
<>
<p>Are you happy?</p>
<AnswerButton onYes={onAnswerYes} onNo={onAnswerNo} />
<p style={{ "font-size": "50px" }}>{isHappy() ? "😀" : "😥"}</p>
</>
);
}
Slot
Angular
import { Component, NgModule } from "@angular/core";
import { FunnyButtonModule } from "./funny-button.component";
@Component({
selector: "app-root",
template: `<app-funny-button>Click me!</app-funny-button>`,
})
export class AppComponent {}
@NgModule({
declarations: [AppComponent],
imports: [FunnyButtonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Solid.js
import FunnyButton from "./FunnyButton.jsx";
export default function App() {
return <FunnyButton>Click me!</FunnyButton>;
}
Slot fallback
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-root",
template: `
<app-funny-button></app-funny-button>
<app-funny-button>
<ng-template #content>I got content!</ng-template>
</app-funny-button>
`,
})
export class AppComponent {}
@NgModule({
declarations: [AppComponent],
imports: [FunnyButtonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Solid.js
import FunnyButton from "./FunnyButton.jsx";
export default function App() {
return (
<>
<FunnyButton />
<FunnyButton>I got content!</FunnyButton>
</>
);
}
Context
Angular
import { Component, NgModule } from "@angular/core";
import { UserService } from "./user.service";
import { UserProfileModule } from "./user-profile.component";
@Component({
providers: [UserService],
selector: "app-root",
template: `
<h1>Welcome back, {{ userService.user.username }}</h1>
<app-user-profile />
`,
})
export class AppComponent {
constructor(public userService: UserService) {}
}
@NgModule({
declarations: [AppComponent],
imports: [UserProfileModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Solid.js
import { createSignal } from "solid-js";
import { UserContext } from "./UserContext";
import UserProfile from "./UserProfile";
export default function App() {
const [user, setUser] = createSignal({
id: 1,
username: "unicorn42",
email: "unicorn42@example.com",
});
function updateUsername(newUsername) {
setUser({ ...user(), username: newUsername });
}
return (
<>
<h1>Welcome back, {user().username}</h1>
<UserContext.Provider value={[user, updateUsername]}>
<UserProfile />
</UserContext.Provider>
</>
);
}
Form input
Input text
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-input-hello",
template: `
<p>{{ text }}</p>
<input [(ngModel)]="text" />
`,
})
export class InputHelloComponent {
text = "";
}
@NgModule({
declarations: [InputHelloComponent],
imports: [FormsModule],
exports: [InputHelloComponent],
})
export class InputHelloModule {}
Solid.js
import { createSignal } from "solid-js";
export default function InputHello() {
const [text, setText] = createSignal("Hello world");
function handleChange(event) {
setText(event.target.value);
}
return (
<>
<p>{text()}</p>
<input value={text()} onInput={handleChange} />
</>
);
}
Checkbox
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-is-available",
template: `
<input id="is-available" type="checkbox" [(ngModel)]="isAvailable" />
<label for="is-available">Is available</label>
`,
})
export class IsAvailableComponent {
isAvailable = false;
}
@NgModule({
declarations: [IsAvailableComponent],
imports: [FormsModule],
exports: [IsAvailableComponent],
})
export class IsAvailableModule {}
Solid.js
import { createSignal } from "solid-js";
export default function IsAvailable() {
const [isAvailable, setIsAvailable] = createSignal(false);
function handleChange() {
setIsAvailable((previousValue) => !previousValue);
}
return (
<>
<input
id="is-available"
type="checkbox"
checked={isAvailable()}
onChange={handleChange}
/>
<label for="is-available">Is available</label>
</>
);
}
Radio
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-pick-pill",
template: `
<div>Picked: {{ picked }}</div>
<input id="blue-pill" type="radio" value="blue" [(ngModel)]="picked" />
<label for="blue-pill">Blue pill</label>
<input id="red-pill" type="radio" value="red" [(ngModel)]="picked" />
<label for="red-pill">Red pill</label>
`,
})
export class PickPillComponent {
picked = "red";
}
@NgModule({
declarations: [PickPillComponent],
imports: [FormsModule],
exports: [PickPillComponent],
})
export class PickPillModule {}
Solid.js
import { createSignal } from "solid-js";
export default function PickPill() {
const [picked, setPicked] = createSignal("red");
function handleChange(event) {
setPicked(event.target.value);
}
return (
<>
<div>Picked: {picked()}</div>
<input
id="blue-pill"
checked={picked() === "blue"}
type="radio"
value="blue"
onChange={handleChange}
/>
<label for="blue-pill">Blue pill</label>
<input
id="red-pill"
checked={picked() === "red"}
type="radio"
value="red"
onChange={handleChange}
/>
<label for="red-pill">Red pill</label>
</>
);
}
Select
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-color-select",
template: `
<select [(ngModel)]="selectedColorId">
<option
*ngFor="let color of colors"
[value]="color.id"
[disabled]="color.isDisabled"
>
{{ color.text }}
</option>
</select>
`,
})
export class ColorSelectComponent {
selectedColorId = 2;
colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
}
@NgModule({
declarations: [ColorSelectComponent],
imports: [FormsModule],
exports: [ColorSelectComponent],
})
export class ColorSelectModule {}
Solid.js
import { createSignal, For } from "solid-js";
const colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
export default function ColorSelect() {
const [selectedColorId, setSelectedColorId] = createSignal(2);
function handleChange(event) {
setSelectedColorId(event.target.value);
}
return (
<select value={selectedColorId()} onChange={handleChange}>
<For each={colors}>
{(color) => (
<option value={color.id} disabled={color.isDisabled}>
{color.text}
</option>
)}
</For>
</select>
);
}
Webapp features
Render app
Angular
<!DOCTYPE html>
<html>
<body>
<app-root></app-root>
</body>
</html>
Solid.js
<!DOCTYPE html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.jsx"></script>
</body>
</html>
Fetch data
Angular
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { BehaviorSubject } from "rxjs";
export interface UsersState {
users: User[];
error: string | null;
loading: boolean;
}
export const initialState: UsersState = {
users: [],
error: null,
loading: false,
};
@Injectable({ providedIn: "root" })
export class UserService {
private state = new BehaviorSubject<UsersState>(initialState);
state$ = this.state.asObservable();
constructor(private http: HttpClient) {}
loadUsers() {
this.state.next({ ...initialState, loading: true });
this.http
.get<UserResponse>("https://randomuser.me/api/?results=3")
.subscribe({
next: ({ results }) =>
this.state.next({ ...initialState, users: results }),
error: (error) => this.state.next({ ...initialState, error }),
});
}
}
export interface UserResponse {
results: User[];
info: any;
}
export interface User {
name: {
title: string;
first: string;
last: string;
};
picture: {
large: string;
medium: string;
thumbnail: string;
};
}
Solid.js
import { createResource, For, Switch, Match } from "solid-js";
async function fetchUsers() {
return (await fetch("https://randomuser.me/api/?results=3")).json();
}
export default function App() {
const [data] = createResource(fetchUsers);
const users = () => data()?.results;
return (
<Switch>
<Match when={data.loading}>
<p>Fetching users...</p>
</Match>
<Match when={data.error}>
<p>An error occurred while fetching users</p>
</Match>
<Match when={users()}>
<ul>
<For each={users()}>
{(user) => (
<li>
<img src={user.picture.thumbnail} alt="user" />
<p>
{user.name.first} {user.name.last}
</p>
</li>
)}
</For>
</ul>
</Match>
</Switch>
);
}
Decouvrez plus d’Offres de la plateform ItGalaxy.io :
Découvrez notre gamme complète de services et formations pour accélérer votre carrière.
1. Nous contactez
- Description: Besoin de Formation et des Solutions cloud complètes pour vos applications
- Links:
2. Infra as a Service
- Description: Infrastructure cloud évolutive et sécurisée
- Links:
3. Projets Développeurs
- Description: Découvrez des opportunités passionnantes pour les développeurs
- Links:
4. Développeurs
- Description: Rejoignez notre communauté de développeurs
- Links:
5. Formations Complètes
- Description: Accédez à des formations professionnelles de haute qualité
- Links:
6. Marketplace
- Description: Découvrez notre place de marché de services
- Links:
7. Blogs
- Description: Découvrez nos blogs
- Links:
- comment creer une application mobile ?
- Comment monitorer un site web ?
- Command Checkout in git ?
- Comment git checkout to commit ?
- supprimer une branche git
- dockercoin
- kubernetes c est quoi
- architecture kubernetes
- Installer Gitlab Runner ?
- .gitlab-ci.yml exemples
- CI/CD
- svelte 5 vs solid
- svelte vs lit
- solidjs vs qwik
- alpine vs vue
- Plateform Freelance 2025
- Creation d’un site Web gratuitement
This website is powered by ItGalaxy.io