Angular is a robust and opinionated framework designed for building scalable web applications. It uses TypeScript and follows a component-based approach, separating the interface, data, and logic.
ng new Command in AngularThe Angular CLI’sng new command bootstraps a new Angular project. This command sets up the project structure and configurations necessary for development.
ng new my-angular-app
The Angular CLI simplifies development with several useful commands:
ng generate to create new components, directives, and other Angular elements.ng serve compiles and hosts the application on a local server, automatically refreshing on file changes.ng build compiles and optimizes the application for production.ng generate component user-profileng serveng build
@Component Decorator in AngularThe@Component decorator in Angular defines a component, specifying its selector, template, and styles. Metadata in the decorator specifies how the component should be processed and used at runtime.
import{ Component}from'@angular/core';@Component({standalone:true,selector:'demo-app',templateUrl:'./demo-app.component.html',styleUrl:'./demo-app.component.css',imports:[RouterOutlet]})exportclassDemoApp{}
In Angular, standalone components are configured using thestandalone flag. This enables simplified imports and allows the component to be used without needing explicit module management.
@Component({standalone:true,selector:'demo-app',templateUrl:'./demo-app.component.html',styleUrl:'./demo-app.component.css',imports:[RouterOutlet]})exportclassDemoApp{}
Angular’s template syntax allows dynamic content rendering using data bindings. Two forms are interpolation and property binding.
Interpolation inserts text into the template with{{ value }}, while property binding dynamically sets element properties using[property]="value".
In the example code,userName is inserted using interpolation such as{{ userName }}, andsrc is set using property binding such as[src]="user.profileImage".
exportclassUserProfileComponent{userName='Rajesh Kumar';user={ profileImage:'path/to/image.jpg'};}<!-- using interpolation and property bindingin the component template--><div>Hello,{{ userName}}!</div><img[src]="user.profileImage">
Angular templates use control-flow structures like@if,@for, and@switch to manage dynamic content display.
@if conditionally includes HTML based on a boolean expression.@if (a > b) {{{a}} is greater than {{b}}} @else if (b > a) {{{a}} is less than {{b}}} @else {{{a}} is equal to {{b}}}
@for iterates over collections to generate repeated elements.@for (item of items; track item.name) {<li>{{ item.name }}</li>} @empty {<li>There are no items.</li>}
@switch renders elements based on the value of an expression, handling multiple conditions.@switch (condition) {@case (caseA) {Case A.}@case (caseB) {Case B.}@default {Default case.}}
In Angular, template variables store references to DOM elements, allowing easy access and manipulation within the template.
<!-- add template reference variable named userInput --><input#userInputtype="text"placeholder="Enter your name"value="Codey"><!-- use interpolation to display the current value of the input field referenced with #userInput. --><h2>{{ userInput.value }}</h2>
Data binding in Angular synchronizes data between the application model and the view. This ensures that any changes in the model are automatically reflected in the UI, eliminating the need for manual DOM updates.
In Angular,<ng-content> enables content projection, allowing a parent component to project content into a child component. This enhances layout flexibility by allowing developers to construct child components that are more generic.
Child component template:
<!-- menu-item.component.html --><divclass="menu-item"><ng-content></ng-content></div>
Parent component template:
<!-- menu.component.html --><app-menu-item><p>Projected Content</p></app-menu-item>
select Attribute of<ng-content>For multi-slot content projection in Angular, use multiple<ng-content> tags with correspondingselect attributes to project content based on CSS selectors.
Child component template:
<!-- complex-menu.component.html --><divclass="menu-header"><ng-contentselect=".headerContent"></ng-content><!-- First slot --></div><divclass="menu-body"><ng-contentselect=".bodyContent"></ng-content><!-- Second slot --></div>
Parent component template:
<!-- menu.component.html --><app-menu-item><divclass="headerContent">Header Content</div><!-- First slot --><divclass="bodyContent">Body Content</div><!-- Second slot --></app-menu-item>
NgOptimizedImage Directive in AngularTheNgOptimizedImage directive in Angular automatically optimizes images for different screen sizes and conditions, improving load times and resource efficiency.
import{ NgOptimizedImage}from'@angular/common';@Component({selector:'app-image-display',standalone:true,imports:[NgOptimizedImage],template:`<img [ngSrc]="user.profilePictureUrl" width="200" height="200" alt="User Profile Picture">`,styleUrl:'./image-display.component.css'})
ngSrc AttributeThengSrc attribute binds image sources dynamically within Angular templates. Ensure that the width and height of the image are specified, or use thefill attribute to make the image fill the containing element.
<!-- Dynamically binding an image source in Angular --><img[ngSrc]="user.profilePictureUrl"width="200"height="200"alt="User Profile Picture">