You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's recreate the "Magical Users" app in Angular. As we go along, I'll explain each Angular concept in-depth, and contrast it with the React approach from the given exercise.
#161
Let's recreate the "Magical Users" app in Angular. As we go along, I'll explain each Angular concept in-depth, and contrast it with the React approach from the given exercise.
Creating "Magical Users" App in Angular
Step 1: Setup
React: Initialized the React app using Create React App.
Angular: Initialize a new Angular app using Angular CLI.
ng new magical-users
Navigate into the project directory:
cd magical-users
Start the development server:
ng serve
Open your browser and go to http://localhost:4200 to see the default Angular app running.
Step 2: Install Axios
React: Installed Axios via npm.
Angular: Angular recommends using the HttpClient module for HTTP requests. So, we'll set that up.
First, open src/app/app.module.ts and import HttpClientModule from @angular/common/http. Add HttpClientModule to the imports array.
Step 3: Clear the default project and Fetch Users
React: Cleared src/App.js and fetched users using Axios.
The styling remains largely the same. Add the provided styles to src/styles.css.
Step 6: Run the app
With ng serve running, visit http://localhost:4200. You should see the app functioning similarly to the React version.
Angular Concepts & Contrasts with React:
Angular CLI vs. Create React App:
Angular CLI is a robust tool to scaffold, develop, and maintain Angular apps. React uses Create React App for bootstrapping projects.
Angular Components vs. React Components:
Both frameworks use components as core building blocks.
React uses function or class components, while Angular uses a class with associated metadata.
HttpClient vs. Axios:
In React, Axios is a popular choice for HTTP requests. In Angular, the framework provides the HttpClient module.
State & Lifecycle:
React has useState and lifecycle methods/hooks like useEffect.
Angular components have lifecycle hooks like ngOnInit (similar to componentDidMount) and properties for state.
Input & Props:
In React, data is passed to child components via props.
Angular uses the @Input() decorator to receive data from parent components.
Event Handling:
React uses camelCase event names (onClick).
Angular uses event binding syntax ((click)).
Conditional Rendering:
React uses conditional rendering with JavaScript logic (&& or ternary operators).
Angular uses structural directives like *ngIf.
Styling:
Both React and Angular support component-scoped styles, but the mechanisms differ.
By the end of this exercise, React freshers should grasp Angular's architecture, data flow, component structure, and HTTP mechanisms. Contrasting Angular and React provides a solid understanding, facilitating the transition between frameworks.
Let's recreate the "Magical Users" app in Angular. As we go along, I'll explain each Angular concept in-depth, and contrast it with the React approach from the given exercise.
Creating "Magical Users" App in Angular
Step 1: Setup
cd magical-users
Open your browser and go to
http://localhost:4200
to see the default Angular app running.Step 2: Install Axios
HttpClient
module for HTTP requests. So, we'll set that up.First, open
src/app/app.module.ts
and importHttpClientModule
from@angular/common/http
. AddHttpClientModule
to theimports
array.Step 3: Clear the default project and Fetch Users
src/App.js
and fetched users using Axios.Open
src/app/app.component.ts
:Then, in
src/app/app.component.html
:Step 4: Create UserCard component
UserCard.js
.In
src/app/user-card/user-card.component.ts
:In
src/app/user-card/user-card.component.html
:Step 5: Add some styles
The styling remains largely the same. Add the provided styles to
src/styles.css
.Step 6: Run the app
With
ng serve
running, visithttp://localhost:4200
. You should see the app functioning similarly to the React version.Angular Concepts & Contrasts with React:
Angular CLI vs. Create React App:
Angular Components vs. React Components:
HttpClient vs. Axios:
HttpClient
module.State & Lifecycle:
useState
and lifecycle methods/hooks likeuseEffect
.ngOnInit
(similar tocomponentDidMount
) and properties for state.Input & Props:
@Input()
decorator to receive data from parent components.Event Handling:
onClick
).(click)
).Conditional Rendering:
&&
or ternary operators).*ngIf
.Styling:
By the end of this exercise, React freshers should grasp Angular's architecture, data flow, component structure, and HTTP mechanisms. Contrasting Angular and React provides a solid understanding, facilitating the transition between frameworks.
Originally posted by @akash-coded in #159
The text was updated successfully, but these errors were encountered: