- Interpolation
- Property Binding
- Event Binding
- Two-Way Binding
1. Interpolation
import { Component } from '@angular/core'; @Component({ selector: 'app-interpolation', template: `<h1>{{ title }}</h1>` }) export class InterpolationComponent { title: string = 'Hello, Angular!'; }
2. Property Binding
import { Component } from '@angular/core'; @Component({ selector: 'app-property-binding', template: `<img [src]="imageUrl" alt="Image">` }) export class PropertyBindingComponent { imageUrl: string = 'https://example.com/image.png'; }
3. Event Binding
import { Component } from '@angular/core'; @Component({ selector: 'app-event-binding', template: `<button (click)="onClick()">Click Me!</button>` }) export class EventBindingComponent { onClick() { alert('Button clicked!'); } }
4. Two-Way Binding
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-two-way-binding', template: `<input [(ngModel)]="name" placeholder="Enter your name"> <p>Hello, {{ name }}!</p>` }) export class TwoWayBindingComponent { name: string = ''; }
Angular Data Binding Example.
ng new data-binding-example
cd data-binding-example
ng generate component data-binding
import { Component } from '@angular/core'; @Component({ selector: 'app-data-binding', templateUrl: './data-binding.component.html', styleUrls: ['./data-binding.component.css'] }) export class DataBindingComponent { title: string = 'Data Binding Example'; imageUrl: string = 'https://via.placeholder.com/150'; name: string = ''; onClick() { alert(`Hello, ${this.name}!`); } }
<h1>{{ title }}</h1> <!-- Interpolation --> <img [src]="imageUrl" alt="Placeholder Image"> <!-- Property Binding --> <button (click)="onClick()">Click Me!</button> <!-- Event Binding --> <input [(ngModel)]="name" placeholder="Enter your name"> <!-- Two-Way Binding --> <p>Hello, {{ name }}!</p> <!-- Interpolation -->
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; // Import FormsModule import { AppComponent } from './app.component'; import { DataBindingComponent } from './data-binding/data-binding.component'; @NgModule({ declarations: [ AppComponent, DataBindingComponent ], imports: [ BrowserModule, FormsModule // Add FormsModule to imports ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
<app-data-binding></app-data-binding>
- A title displayed using interpolation.
- An image is displayed using property binding.
- A button that shows an alert with a greeting when clicked (event binding).
- An input field that updates the greeting in real-time using two-way binding.
Summary
- Interpolation: Displays component data in the template.
- Property Binding: Binds component properties to HTML element properties.
- Event Binding: Listens to events and executes component methods.
- Two-Way Binding: Synchronizes data between the component and the view, especially in forms.
Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.