Sure! Here's an example of a simple calculator built using Angular:
Create a new Angular project using the Angular CLI by running the following command in your terminal:
code Copy code
ng new calculator-app
Change into the project directory:
code Copy code
cd calculator-app
Generate a new component for the calculator:
code Copy code
ng generate component calculator
Open the calculator.component.html file and replace its content with the following code:
html Copy code
<div>
<input type="text" [(ngModel)]="firstNumber" placeholder="First number" />
<input type="text" [(ngModel)]="secondNumber" placeholder="Second number" />
<button (click)="add()">Add</button>
<button (click)="subtract()">Subtract</button>
<button (click)="multiply()">Multiply</button>
<button (click)="divide()">Divide</button>
<h4>Result: {{ result }}</h4>
</div>
Open the calculator.component.ts file and replace its content with the following code:
typescript Copy code
import { Component } from '@angular/core';
@Component({
selector: 'app-calculator',
templateUrl: './calculator.component.html',
styleUrls: ['./calculator.component.css']
})
export class CalculatorComponent {
firstNumber: number;
secondNumber: number;
result: number;
add(): void {
this.result = this.firstNumber + this.secondNumber;
}
subtract(): void {
this.result = this.firstNumber - this.secondNumber;
}
multiply(): void {
this.result = this.firstNumber * this.secondNumber;
}
divide(): void {
this.result = this.firstNumber / this.secondNumber;
}
}
Run the application in your browser using the following command:
Open your browser and navigate to http://localhost:4200. You should see the calculator with two input fields, four buttons for arithmetic operations, and a result displayed below.
That's it! You've created a simple calculator in Angular. You can perform addition, subtraction, multiplication, and division by entering two numbers and clicking on the respective buttons. The result will be displayed below.