Angular Components and Templates
Angular Components and Templates:
components are the building blocks of your application's user interface.
Creating Components in Angular
Generate Component: You can choose to use the Angular CLI command 'ng generate component componentName' or 'ng g c componentName' with its nickname.
Component Class: This is a TypeScript class that contains the logic for the component. It typically includes properties and methods that define the component's behavior.
Component Decorator: Components are decorated with '@Component()', which allows you to specify metadata such as the component's selector, template, styles, and more.
Template and Styles: Template proxy specify component's HTML structure and styles define components's CSS styles.
Using Templates to Define Component Views
The templates in Angular are HTML files that create an HTML view used for the components.
It can be done in such a way that it presents Angular-specific approach that deals with component directives which are used to implement component logic.
<!-- app.component.html -->
<div>
<h1>{{ title }}</h1>
<button (click)="onClick()">Click me!</button>
</div>
Data Binding: Interpolation, Property Binding, Event Binding
Interpolation: Interpolation is a way to output data from the component class into the template. It's denoted by double curly braces {{ }}.
<!-- app.component.html -->
<p>{{ message }}</p>
Property Binding: Property binding allows you to set an HTML element's property based on a value in the component class. It's denoted by square brackets [].
<!-- app.component.html -->
<img [src]="imageUrl" />
Event Binding: Event binding allows you to listen for events emitted by HTML elements and trigger methods in the component class. It's denoted by parentheses ().
<!-- app.component.html -->
<button (click)="onButtonClick()">Click me!</button>