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
Copy file name to clipboardExpand all lines: README.md
+165-6Lines changed: 165 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -167,7 +167,7 @@ This repository contains a list of resources to learn Angular. It includes tutor
167
167
168
168
## Introduction
169
169
170
-
Angular is a popular open-source web application framework developed by Google. It is used for building single-page web applications and dynamic web applications. Angular provides a set of tools and libraries for building modern web applications, including components, services, forms, routing, HTTP client, and more. Angular is built using TypeScript, a superset of JavaScript that adds static typing and other features to the language. Angular is known for its performance, scalability, and developer productivity.
170
+
**Angular** is a popular open-source web application framework developed by Google. It is used for building single-page web applications and dynamic web applications. Angular provides a set of tools and libraries for building modern web applications, including components, services, forms, routing, HTTP client, and more. Angular is built using TypeScript, a superset of JavaScript that adds static typing and other features to the language. Angular is known for its performance, scalability, and developer productivity.
171
171
172
172
### Features of Angular
173
173
@@ -225,7 +225,7 @@ Angular is a popular open-source web application framework developed by Google.
225
225
226
226
## SPA
227
227
228
-
SPA stands for Single Page Application. It is a web application or website that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from the server. This approach allows for a more fluid and responsive user experience, as the page does not need to be reloaded each time the user interacts with it.
228
+
**SPA** stands for **Single Page Application**. It is a web application or website that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from the server. This approach allows for a more fluid and responsive user experience, as the page does not need to be reloaded each time the user interacts with it.
229
229
230
230
### Advantages of SPA
231
231
@@ -254,6 +254,8 @@ SPA stands for Single Page Application. It is a web application or website that
254
254
### Prerequisites
255
255
256
256
- Node.js
257
+
- NPM
258
+
- Angular CLI
257
259
258
260
### Installation
259
261
@@ -263,13 +265,19 @@ Install the Angular CLI globally:
263
265
npm install -g @angular/cli
264
266
```
265
267
268
+
If you have already installed the Angular CLI, you can update it to the latest version using the following command:
269
+
270
+
```bash
271
+
npm install -g @angular/cli@latest
272
+
```
273
+
266
274
Check version
267
275
268
276
```bash
269
277
ng version
270
278
```
271
279
272
-
Create workspace:
280
+
Create a new Angular project: (Replace `[PROJECT NAME]` with your project name)
273
281
274
282
```bash
275
283
# with standalone component
@@ -279,7 +287,7 @@ ng new [PROJECT NAME]
279
287
ng new [PROJECT NAME] --standalone=false
280
288
```
281
289
282
-
Note: In version v17 and later, the standalone component is default enabled. In version v16 and earlier, the standalone component is disabled by default. You can enable or disable the standalone component using the `--standalone` flag.
290
+
**Note**: In version v17 and later, the standalone component is default enabled. In version v16 and earlier, the standalone component is disabled by default. You can enable or disable the standalone component using the `--standalone` flag. (In this repository, an example repository is created with the latest version of Angular.)
283
291
284
292
Navigate to the project directory:
285
293
@@ -383,11 +391,14 @@ import { Component } from '@angular/core';
383
391
```typescript
384
392
@Component({
385
393
selector: 'app-[component-name]',
394
+
standalone: true,
386
395
templateUrl: './[component-name].component.html',
387
396
styleUrls: ['./[component-name].component.css']
388
397
})
389
398
```
390
399
400
+
If you want to create a standalone component, set the `standalone` property to `true`. If you want to create a non-standalone component, set the `standalone` property to `false`. The standalone component is enabled by default in Angular v17 and later.
401
+
391
402
**Step 7** - Define the selector, template, and styles for the component.
392
403
393
404
```text
@@ -432,7 +443,9 @@ ng serve
432
443
433
444
### Example
434
445
435
-
**Creating the component files** -
446
+
**Creating the component files (Version 16 and earlier)** -
447
+
448
+
```bash
436
449
437
450
```typescript
438
451
//test-component.component.ts
@@ -461,7 +474,39 @@ h1 {
461
474
}
462
475
```
463
476
464
-
**Importing the component in the app.module.ts file** -
477
+
**Creating the component files (Version 17 and later)** -
478
+
479
+
```bash
480
+
//test-component.component.ts
481
+
import { Component } from '@angular/core';
482
+
483
+
@Component({
484
+
selector: 'app-test-component',
485
+
standalone: true,
486
+
templateUrl: './test-component.component.html',
487
+
styleUrls: ['./test-component.component.css']
488
+
})
489
+
490
+
export class TestComponent {
491
+
492
+
}
493
+
```
494
+
495
+
You can create a standalone component by setting the `standalone` property to `true`. The standalone component is enabled by default in Angular v17 and later. You can disable the standalone component by setting the `standalone` property to `false` in the `@Component` decorator of the component. If you disable the standalone component, you need to import the component in the `app.module.ts` file. If you created a non-standalone component, you will see no standalone property in the `@Component` decorator.
496
+
497
+
```html
498
+
<!--test-component.component.html-->
499
+
<h1>Test Component</h1>
500
+
```
501
+
502
+
```css
503
+
/*test-component.component.css*/
504
+
h1 {
505
+
color: red;
506
+
}
507
+
```
508
+
509
+
**Importing the component in the app.module.ts file (Version 16 and earlier)** -
465
510
466
511
```typescript
467
512
//app.module.ts
@@ -485,6 +530,71 @@ import { TestComponent } from './app.component';
485
530
export class AppModule { }
486
531
```
487
532
533
+
**Importing the component in the app.module.ts file (Version 17 and later without standalone component)** -
534
+
535
+
```typescript
536
+
//app.module.ts
537
+
import { BrowserModule } from '@angular/platform-browser';
538
+
import { NgModule } from '@angular/core';
539
+
540
+
import { AppComponent } from './app.component';
541
+
import { TestComponent } from './test-component/test-component.component';
542
+
543
+
@NgModule({
544
+
declarations: [
545
+
AppComponent,
546
+
TestComponent
547
+
],
548
+
imports: [
549
+
BrowserModule
550
+
],
551
+
providers: [],
552
+
bootstrap: [AppComponent]
553
+
})
554
+
export class AppModule { }
555
+
```
556
+
557
+
In version 17 and later, the standalone component is enabled by default. You can disable the standalone component by setting the `standalone` property to `false`. Inside app folder, `app.config.ts` file is created by default.
558
+
559
+
```typescript
560
+
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
You can import the component in the `app.component.ts` file and use the component selector in the HTML template.
574
+
575
+
```typescript
576
+
//app.component.ts
577
+
import { Component } from '@angular/core';
578
+
import { RouterOutlet } from '@angular/router';
579
+
import { TestComponent } from './test-component/test-component.component';
580
+
581
+
@Component({
582
+
selector: 'app-root',
583
+
standalone: true,
584
+
imports: [TestComponent],
585
+
templateUrl: './app.component.html',
586
+
styleUrl: './app.component.scss'
587
+
})
588
+
export class AppComponent {
589
+
title = 'app';
590
+
}
591
+
```
592
+
593
+
```html
594
+
<!--app.component.html-->
595
+
<app-test-component></app-test-component>
596
+
```
597
+
488
598
**Creating the inline Template & StyleUrls** -
489
599
490
600
```typescript
@@ -529,6 +639,30 @@ function testFunction() {
529
639
testFunction();
530
640
```
531
641
642
+
Example in Angular :
643
+
644
+
```typescript
645
+
import { Component } from '@angular/core';
646
+
647
+
// Global Scope
648
+
let globalVariable = 'Global Variable';
649
+
650
+
@Component({
651
+
selector: 'app-root',
652
+
templateUrl: './app.component.html',
653
+
styleUrls: ['./app.component.css']
654
+
})
655
+
656
+
export class AppComponent {
657
+
title = globalVariable;
658
+
}
659
+
```
660
+
661
+
```html
662
+
<!--app.component.html-->
663
+
<h1>{{ title }}</h1>
664
+
```
665
+
532
666
### Local Scope
533
667
534
668
Variables defined in a function or block of code are accessible only within that function or block. They are not accessible outside of the function or block.
@@ -546,6 +680,31 @@ testFunction();
546
680
console.log(localVariable); // Error: localVariable is not defined
547
681
```
548
682
683
+
Example in Angular :
684
+
685
+
```typescript
686
+
import { Component } from '@angular/core';
687
+
688
+
@Component({
689
+
selector: 'app-root',
690
+
templateUrl: './app.component.html',
691
+
styleUrls: ['./app.component.css']
692
+
})
693
+
694
+
export class AppComponent {
695
+
testFunction() {
696
+
// Local Scope
697
+
let localVariable = 'Local Variable';
698
+
console.log(localVariable); // Output: Local Variable
Variables defined in an Angular component are accessible within that component and its child components. They are not accessible outside of the component.
0 commit comments