Week 11 - AWAD
Week 11 - AWAD
Ans: - Data binding in Angular is a powerful mechanism that allows communication between a
component's class (the TypeScript code) and its template (the HTML). Angular provides several types
of data binding to cater to different use cases. These types include:
Interpolation:
Syntax: {{ expression }}
Description: Interpolation allows you to embed expressions within double curly braces in the
template. It evaluates the expression and updates the DOM with the resulting value.
Example:
Property Binding:
Syntax: [property]="expression"
Description: Property binding enables you to bind data from the component to an HTML property. It
uses square brackets to bind the component property to the HTML attribute.
Example:
imageUrl = 'path/to/image.jpg';
Attribute Binding:
Syntax: [attr.attribute-name]="expression"
Description: Attribute binding is used to bind data to an HTML attribute that is not a property of an
element. This is useful for attributes like aria-*, role, etc.
Example:
<button [attr.aria-label]="ariaLabel">Click me</button>
Class Binding:
Syntax: [class.class-name]="expression"
Description: Class binding lets you add or remove CSS classes conditionally.
Example:
<div [class.active]="isActive">Content</div>
isActive = true;
Style Binding:
Syntax: [style.style-property]="expression"
Example:
bgColor = 'lightblue';
Event Binding:
Syntax: (event)="handler"
Description: Event binding is used to listen for and respond to DOM events. It uses parentheses
around the event name to bind an event to a method in the component.
Example:
handleClick() {
console.log('Button clicked!');
Syntax: [(ngModel)]="property"
Description: Two-way data binding combines property and event binding to synchronize data
between the component class and the template. It uses the ngModel directive inside a banana-in-a-
box syntax [()].
Example:
name = 'Angular';
These types of data binding offer a flexible and efficient way to manage the flow of data between the
component class and the template, ensuring that the user interface is always in sync with the
underlying data model.
Ans: - Creating an Angular application using the Angular CLI (Command Line Interface) is a
straightforward and efficient process. The Angular CLI provides a powerful set of commands to
scaffold, develop, and maintain Angular applications. Here’s a step-by-step guide on how to create an
Angular application using the Angular CLI:
First, you need to install the Angular CLI globally on your system. You can do this using npm (Node
Package Manager). Open your terminal or command prompt and run the following command:
Once the Angular CLI is installed, you can create a new Angular application using the ng new
command followed by the name of your application. For example, to create an application named
my-angular-app, run:
ng new my-angular-app
Which stylesheet format would you like to use? (CSS, SCSS, SASS, LESS, Stylus)
Answer these prompts based on your preferences. The CLI will then generate a new directory named
my-angular-app with the initial project structure and dependencies.
cd my-angular-app
To serve the application locally and start a development server, use the ng serve command:
ng serve
By default, the application will be available at http://localhost:4200/. Open this URL in your web
browser to see your new Angular application running.
The Angular CLI sets up a project with a well-defined structure. Here are some key directories and
files you’ll find:
The Angular CLI provides commands to generate components, services, modules, and other Angular
entities. For example, to generate a new component called my-component, use:
ng generate component my-component
ng g c my-component
Or, shorthand:
ng g s my-service
ng build --prod
This command compiles the application into an optimized, minified output in the dist/ directory,
ready for deployment.