Angular - One-way Binding - Event Binding - Live Calculator app #17
Replies: 14 comments
-
app.component.html<div class="container">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>First Number:</label>
<input type="number" class="form-control" [(ngModel)]="num1" />
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Second Number:</label>
<input type="number" class="form-control" [(ngModel)]="num2" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Enter Choice:</label>
<input type="number" class="form-control" [(ngModel)]="oper" />
</div>
</div>
<button type="submit" (click)="calculate()">CALCULATE</button>
</div>
<p>
First Number:<strong class="text-uppercase">{{ num1 }}</strong>
</p>
<p>
Secon Number:<strong class="text-uppercase">{{ num2 }}</strong>
</p>
<p>
Result:<strong class="text-uppercase">{{ result }}</strong>
</p>
</div>app.component.tsimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public num1 : number = 0;
public num2: number = 0;
public oper : number = 0;
public result : number = 0;
public firstName: string = '';
public lastName: string = '';
public position: string = '';
public salary: number = 0;
constructor() {}
ngOnInit(): void {}
public calculate() {
let num1 = this.num1;
let num2 = this.num2;
let operation = this.oper;
switch (operation){
case 1:
this.result = num1 + num2;
break;
case 2:
this.result = num1 - num2;
break;
case 3:
this.result = num1 * num2;
break;
case 4:
this.result = num1 / num2;
break;
case 5:
this.result = num1 % num2;
break;
case 6:
alert("Goodbye.");
break;
default:
alert("Invalid choice!");
break;
}
}
}screenshot |
Beta Was this translation helpful? Give feedback.
-
app.component.html<!DOCTYPE html>
<html lang="en">
<head> </head>
<body>
<p>Calculator</p>
<p>1.Addition</p>
<p>2.Substraction</p>
<p>3.Multiplication</p>
<p>4.Division</p>
<input [(ngModel)]="num1" placeholder="Enter number" />
<input [(ngModel)]="num2" placeholder="Enter number" />
<input [(ngModel)]="choice" placeholder="Enter choice" />
<button (click)="Calculate()">Calculate</button>
<p>{{ num1 }}</p>
<p>{{ num2 }}</p>
<p>result: {{ result }}</p>
</body>
</html>app.component.tsimport { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
num1 : number =0;
num2 : number =0;
choice : number =0;
result :number =0;
Calculate()
{
if(this.choice==1)
{
this.result = this.num1+ this.num2;
}
else if(this.choice==2)
{
this.result = this.num1- this.num2;
}
else if(this.choice==3)
{
this.result = this.num1*this.num2;
}
else if(this.choice==4)
{
this.result = this.num1/this.num2;
}
this.num1=this.num2=0;
}
constructor() { }
ngOnInit(): void {
}
} |
Beta Was this translation helpful? Give feedback.
-
APPS.COMPONENT.HTML<div>
<h1 style="text-align: center">WELCOME TO MY CALCULATOR!</h1>
</div>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>First Number:</label>
<input type="number" class="form-control" [(ngModel)]="num1" />
</div>
</div>
<br />
<div class="col-md-4">
<div class="form-group">
<label>Second Number:</label>
<input type="number" class="form-control" [(ngModel)]="num2" />
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Enter Choice:</label>
<input type="number" class="form-control" [(ngModel)]="oper" />
</div>
</div>
<br />
<div>
<p>Enter a choice between [1-6]</p>
<p>1.Addition (+)</p>
<p>2.Subtraction (-)</p>
<p>3.Multiplication (*)</p>
<p>4.Division(/)</p>
<p>5.Modulus(%)</p>
<p>6.Quit</p>
</div>
<button type="submit" (click)="calculate()">CALCULATE</button>
</div>
<p>
First Number:<strong class="text-uppercase">{{ num1 }}</strong>
</p>
<p>
Second Number:<strong class="text-uppercase">{{ num2 }}</strong>
</p>
<p>
Result:<strong class="text-uppercase">{{ result }}</strong>
</p>
</div>APPS.COMPONENT.TSimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public num1 : number = 0;
public num2: number = 0;
public oper : number = 0;
public result : number = 0;
public firstName: string = '';
public lastName: string = '';
public position: string = '';
public salary: number = 0;
constructor() {}
ngOnInit(): void {}
public calculate() {
let num1 = this.num1;
let num2 = this.num2;
let operation = this.oper;
switch (operation){
case 1:
this.result = num1 + num2;
break;
case 2:
this.result = num1 - num2;
break;
case 3:
this.result = num1 * num2;
break;
case 4:
this.result = num1 / num2;
break;
case 5:
this.result = num1 % num2;
break;
case 6:
alert("Goodbye.");
break;
default:
alert("Invalid choice!");
break;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
app.component.ts import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public num1 : number = 0;
public num2: number = 0;
public operation : number = 0;
public output : number = 0;
constructor() {}
ngOnInit(): void {}
public calculate() {
let num1 = this.num1;
let num2 = this.num2;
let operation = this.operation;
switch (operation){
case 1:
this.output = num1 + num2;
break;
case 2:
this.output = num1 - num2;
break;
case 3:
this.output = num1 * num2;
break;
case 4:
this.output = num1 / num2;
break;
case 5:
this.output = num1 % num2;
break;
case 6:
break;
default:
alert("Invalid input!");
break;
}
}
}app.component.html <div class="container">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Enter Choice:<br>1. Addition<br/>2. Substraction <br/>3. Multiplicaton<br/>4. Division<br/>5. Modulus<br/>6. Quit<br><br></label>
<label>First Number:</label>
<input type="number" class="form-control" [(ngModel)]="num1" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Second Number:</label>
<input type="number" class="form-control" [(ngModel)]="num2" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<input type="number" class="form-control" [(ngModel)]="operation" />
</div>
</div>
<button type="submit" (click)="calculate()">CALCULATE</button>
</div>
<p>
First Number:<strong class="text-uppercase">{{ num1 }}</strong>
</p>
<p>
Second Number:<strong class="text-uppercase">{{ num2 }}</strong>
</p>
<p>
Result:<strong class="text-uppercase">{{ output }}</strong>
</p>
</div>screenshot |
Beta Was this translation helpful? Give feedback.
-
calculator.component.html<div>
<h1>Calculator with Html & Js</h1>
<p id="myParagraph">Welcome to the calculator World</p>
<form action="#" class="form-group, col-6 ">
<input type="number" name="num1" id="num1" [(ngModel)]="ndata1" placeholder="enter 1st number" class="col-2"
(keyup)="calculator()" />
<select name="operators" id="operators" [(ngModel)]="operation" (keyup.enter)="calculator()"
(change)="calculator()" (blur)="calculator()" class="col-1 mx-2">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="number" name="num2" id="num2" placeholder="num2" [(ngModel)]="ndata2" (keyup)="calculator()"
class="col-2" />
<!-- <button type="submit" (click)="calculator()">Calculate</button> -->
</form>
<div class="col-6 my-3">
<h3 id="n1">{{ndata1}}</h3>
<h3 id="result">{{operation}}</h3>
<h3 id="n2">{{ndata2}}</h3>
<h3 id="n2">{{result}}</h3>
</div>calculate.componator.tsimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-calculator',
templateUrl: './calculator.component.html',
styleUrls: ['./calculator.component.css']
})
export class CalculatorComponent implements OnInit {
ndata1 : number =0;
ndata2 : number =0;
operation : string ='+';
result :number =0;
public calculator()
{
//let result : number =0 ;
switch (this.operation) {
case '+':
this.result = this.ndata1 + this.ndata2;
break;
case '-':
this.result = this.ndata1 - this.ndata2;
break;
case '*':
this.result = this.ndata1 * this.ndata2;
break;
case '/':
this.result = this.ndata1 / this.ndata2;
break;
case '%':
this.result = this.ndata1 % this.ndata2;
break;
default:
alert("Invalid choice!");
break;
}
}
constructor() { }
ngOnInit(): void {
}
}output |
Beta Was this translation helpful? Give feedback.
-
|
app.component.html app.component.ts |
Beta Was this translation helpful? Give feedback.
-
calculator.html<h1 style="text-align: center">Calculator</h1>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>First Number:</label>
<input type="number" class="form-control" [(ngModel)]="num1" />
</div>
</div>
<br>
<div class="col-md-4">
<div class="form-group">
<label>Second Number:</label>
<input type="number" class="form-control" [(ngModel)]="num2" />
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Enter Choice:</label>
<input type="number" class="form-control" [(ngModel)]="operation" />
</div>
<br>
</div>
<button type="submit" (click)="calculate()">CALCULATE</button>
</div>
<br>
<p>
First Number:<strong class="text-uppercase">{{ num1 }}</strong>
</p>
<p>
Second Number:<strong class="text-uppercase">{{ num2 }}</strong>
</p>
<p>
Result:<strong class="text-uppercase">{{ result }}</strong>
</p>
</div>calculator.component.tsimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-calculator',
templateUrl: './calculator.component.html',
styleUrls: ['./calculator.component.css']
})
export class CalculatorComponent implements OnInit {
num1: number = 0;
num2: number = 0;
operation: number = 0;
result: number = 0;
constructor() { }
ngOnInit(): void { }
public calculate() {
let num1 = this.num1;
let num2 = this.num2;
let operation = this.operation;
switch (operation) {
case 1:
this.result = num1 + num2;
break;
case 2:
this.result = num1 - num2;
break;
case 3:
this.result = num1 * num2;
break;
case 4:
this.result = num1 / num2;
break;
case 5:
this.result = num1 % num2;
break;
case 6:
alert("Goodbye.");
break;
default:
alert("Invalid choice!");
break;
}
}
}
|
Beta Was this translation helpful? Give feedback.
-
calculator.component.html<!DOCTYPE html>
<html lang="en">
<body>
<p>Calculator</p>
<p>1.Addition</p>
<p>2.Substraction</p>
<p>3.Multiplication</p>
<p>4.Division</p>
<input type="number" [(ngModel)]="num1" placeholder="Enter number" />
<input type="number" [(ngModel)]="num2" placeholder="Enter number" />
<input type="number" [(ngModel)]="choice" placeholder="Enter choice" />
<button (click)="Calculate()">Calculate</button>
<p>{{ num1 }}</p>
<p>{{ num2 }}</p>
<p>result: {{ result }}</p>
</body>
</html>calculator.component.tsimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-calculator',
templateUrl: './calculator.component.html',
styleUrls: ['./calculator.component.css']
})
export class CalculatorComponent implements OnInit {
num1 : number =0;
num2 : number =0;
choice : number =0;
result :number =0;
Calculate()
{
if(this.choice==1)
{
this.result = this.num1 + this.num2;
}
else if(this.choice==2)
{
this.result = this.num1- this.num2;
}
else if(this.choice==3)
{
this.result = this.num1*this.num2;
}
else if(this.choice==4)
{
this.result = this.num1/this.num2;
}
this.num1=this.num2=0;
}
constructor() { }
ngOnInit(): void {
}
}screenchot |
Beta Was this translation helpful? Give feedback.
-
|
app.component.ts import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'live-calc';
num1 : number =0;
num2 : number =0;
choice : number =0;
result :number =0;
Calculate()
{
if(this.choice==1)
{
this.result = this.num1 + this.num2;
}
else if(this.choice==2)
{
this.result = this.num1- this.num2;
}
else if(this.choice==3)
{
this.result = this.num1*this.num2;
}
else if(this.choice==4)
{
this.result = this.num1/this.num2;
}
}
constructor() { }
ngOnInit(): void {
}
}app.component.html <!DOCTYPE html>
<html lang="en">
<body>
<p> Live Calculator</p>
<p>1.Addition</p>
<p>2.Substraction</p>
<p>3.Multiplication</p>
<p>4.Division</p>
<p>5.Modulus</p>
<input type="number" [(ngModel)]="num1" placeholder="Enter number" />
<input type="number" [(ngModel)]="num2" placeholder="Enter number" />
<input type="number" [(ngModel)]="choice" placeholder="Enter choice" />
<button (click)="Calculate()">Calculate</button>
<p>{{ num1 }}</p>
<p>{{ num2 }}</p>
<p>result: {{ result }}</p>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
app.component.html` <style> Calculator{{input}}
{{result}}
C
%
←
/
app.component.ts`import { Component } from '@angular/core'; @component({ |
Beta Was this translation helpful? Give feedback.
-
|
html file <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bootstrap_Form_A</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
</head>
<body>
<div class="container col-md-5 col-lg-6">
<form
action="#"
method="GET"
class="container font-weight-bold mx-auto text-light"
>
<div class="container bg-secondary my-4">
<div class="text-light text-center">
<h1 class="text-uppercase font-weight-bold">Online Calculator</h1>
</div>
<!-- NUMBER-1 -->
<section class="form-group">
<label for="number1">Number1:</label>
<input
type="number"
name="number1"
id="number1"
class="form-control"
placeholder="enter a number"
[(ngModel)]="number1"
/>
</section>
<!-- OPERATOR DROPDOWN -->
<section class="form-group">
<label for="Operator">Operator:</label>
<select
name="operator"
id="operator"
class="form-control"
[(ngModel)]="operator"
>
<option value="" selected>Select Operator</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</section>
<!-- NUMBER-2 -->
<section class="form-group">
<label for="number2">Number2:</label>
<input
type="number"
name="number2"
id="number2"
class="form-control"
placeholder="enter a number"
[(ngModel)]="number2"
/>
</section>
<section class="form-group">
<input
type="button"
class="form-control btn btn-block bg-primary text-uppercase text-light font-weight-bold my-3"
value="Calculate"
(click)="calculate()"
/>
</section>
<section class="form-group">
<h3>
Result of {{ number1 }} {{ operator }} {{ number2 }} is
{{ result }}
</h3>
<br />
</section>
</div>
</form>
</div>
</body>
</html>ts file |
Beta Was this translation helpful? Give feedback.
-
<h1 class="m-5 p-4 text-center text-success">ONLINE CALCULATOR</h1>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5 col-lg-6">
<div>
<div class="form-group">
<label for="number1">Number 1</label>
<input type="number" class="form-control" id="number1"
#number1
(keyup.enter) = "numberOneAdd(number1.value); number1.value = ''"
(blur) = "numberOneAdd(number1.value); number1.value = ''"
placeholder="Enter the number">
</div>
<div class="form-group">
<label for="">Select options</label>
<select [(ngModel)]="selectedLevel" (change)="selected()">
<option *ngFor="let item of data" [ngValue]="item">{{item}}</option>
</select>
{{selectedLevel}}
</div>
<div class="form-group">
<label for="number2">Number 2</label>
<input type="number" class="form-control" id="number2"
#number2
(keyup.enter) = "numberTwoAdd(number2.value); number2.value = ''"
(blur) = "numberTwoAdd(number2.value); number2.value = '' "
placeholder="Enter the number">
</div>
<!-- <div class="col-md-12 text-center">
<button type="submit" class="btn btn-primary btn-block">Calculate</button>
</div> -->
<div id="loginButton" class="my-4">
<button type="button" class="btn btn-success btn-block" (click) = "calculateOperation()">submit</button>
</div>
<h2>Result {{result}}</h2>
</div>
</div>
</div>import { Component, OnInit } from '@angular/core';
import { ɵINTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS } from '@angular/platform-browser-dynamic';
@Component({
selector: 'app-calculator',
templateUrl: './calculator.component.html',
styleUrls: ['./calculator.component.css']
})
export class CalculatorComponent implements OnInit {
num1:number = 0;
num2:number = 0;
result : number = 0;
numberOneAdd(number1 : string){
this.num1 = Number(number1);
}
numberTwoAdd(number2: string){
this.num2 = Number(number2);
}
selectedLevel:string = '';
data:Array<string> = [
"+",
"-",
"*",
"/",
"%"
];
selected(){
console.log(this.selectedLevel)
}
calculateOperation(){
let data:string = this.selectedLevel;
switch(data){
case '+':
this.result = this.num1 + this.num2;
break;
case '-':
this.result = this.num1 - this.num2;
break;
case '*':
this.result = this.num1 * this.num2;
break;
case '/':
this.result = this.num1 / this.num2;
break;
case '%':
this.result = this.num1 % this.num2;
break;
default:
break;
}
}
constructor() { }
ngOnInit(): void {
}
}
|
Beta Was this translation helpful? Give feedback.
-
Calculate Ts Fileimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-calculator',
templateUrl: './calculator.component.html',
styleUrls: ['./calculator.component.css']
})
export class CalculatorComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
result:number = 0;
num1: number = 0 ;
num2 : number = 0 ;
calculate (fname: string , sname:string , operation:string){
this.num1 = Number(fname) ;
this.num2 = Number(sname)
switch(operation){
case "*" :
this.result = this.num1 * this.num2 ;
break ;
case "-" :
this.result = this.num1 - this.num2 ;
break ;
case "+" :
this.result =this.num1 + this.num2 ;
break ;
case "/" :
this.result = this.num1 / this.num2 ;
break ;
}
}
}
Calculator Html File<div class="col-lg-4 offset-4 border border-primary mt-5">
<div class="form-group">
<label for="fNumber">Enter First Number</label>
<input #fname type="number" class="form-control" id="fNumber" aria-describedby="emailHelp" placeholder="First Number">
</div>
<div class="form-group">
<label for="sNumber">Enter Second Number</label>
<input #sname type="number" class="form-control" id="sNumber" placeholder="Second Number">
</div>
<div class="form-check">
<select #operation name="operation" id="operation">
<option value="*">*</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="/">/</option>
</select>
</div>
<div>
Result : {{result}}
</div>
<button class="btn btn-primary" (click) = "calculate(sname.value , fname.value , operation.value);" >Calculate</button>
</div> |
Beta Was this translation helpful? Give feedback.
-
//APP.COMPONENT.HTML
<div>
<input #operation1 type="text" value="">Enter 1st value
<input #operation2 type="text" value="">Enter 2nd value
<select #operation name="operation" id="operation">
<option value="*">*</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="/">/</option>
</select>
<button (click)="handle(operation1.value,operation2.value,operation.value )">submit</button>
<h1>{{result}}</h1>
</div>
//APP.COMPONENT.JS
export class AppComponent{
result:number = 0;
num1: number = 0 ;
num2 : number = 0 ;
handle (fname: string , sname:string , operation:string){
this.num1 = Number(fname) ;
this.num2 = Number(sname)
switch(operation){
case "*" :
this.result = this.num1 * this.num2 ;
break ;
case "-" :
this.result = this.num1 - this.num2 ;
break ;
case "+" :
this.result =this.num1 + this.num2 ;
break ;
case "/" :
this.result = this.num1 / this.num2 ;
break ;
}
}
} |
Beta Was this translation helpful? Give feedback.







Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Convert this static calculator webpage to a live calculator Angular app where the result will be updated in real-time using event binding.
Beta Was this translation helpful? Give feedback.
All reactions