4. 1 Angular Property Binding,

 AIM: Binding image with class property using property binding.


download image.jpg to src\assets folder

Update component.ts with


imageUrl: string = '/assets/image.jpg';

in export Class


add the following line to component.html file

<img [src]="imageUrl" alt="Image">


4.3  Style and Event Binding

Binding an element using inline style and user actions like entering text in input fields. ANGULAR


  1. Style Binding:

Assume you have an input field and a paragraph element. You want to change the paragraph's color based on the text entered in the input field.


app.component.html:

<input type="text" (input)="onInput($event)"> <p [style.color]="textColor">This text changes color</p>

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  textColor = 'black'; // Initial text color

  onInput(event: any) {
    // Assuming you want to set the text color based on the input value
    if (event.target.value === 'red') {
      this.textColor = 'red';
    } else if (event.target.value === 'blue') {
      this.textColor = 'blue';
    } else {
      this.textColor = 'black';
    }
  }
}



In this example, the (input) event is used to detect changes in the input field's value. The onInput method is then called, where you can define the logic to change the textColor property. The [style.color] binding is used to dynamically apply the specified color to the paragraph element.

  1. 2. Event Binding:

Now let's handle a button click event to perform an action when the button is clicked.

app.component.html:

<input type="text" [(ngModel)]="inputText">

<button (click)="displayText()">Show Text</button>

<p>{{ displayedText }}</p>

(To make this work, include forms modules in module.ts file)

app.component.ts:

import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { inputText: string = ''; // Input text from user displayedText: string = ''; // Displayed text displayText() { this.displayedText = this.inputText; } }

In this example, the [(ngModel)] directive is used to bind the value of the input field to the inputText property in the component. The (click) event on the button is used to trigger the displayText method when the button is clicked. This method sets the value of displayedText, which is then displayed in the <p> element.

Remember to include the necessary module imports for FormsModule

like:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms'; // Import FormsModule

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Comments

Popular posts from this blog

9. Angular and MongoDBs

1. a. Angular Application Setup

2. Structural Directives - ngIf