{"id":3922,"date":"2023-11-04T23:13:56","date_gmt":"2023-11-04T23:13:56","guid":{"rendered":"http:\/\/localhost:10003\/how-to-build-a-calculator-app-with-angular-and-rxjs\/"},"modified":"2023-11-05T05:48:27","modified_gmt":"2023-11-05T05:48:27","slug":"how-to-build-a-calculator-app-with-angular-and-rxjs","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-build-a-calculator-app-with-angular-and-rxjs\/","title":{"rendered":"How to Build a Calculator App with Angular and RxJS"},"content":{"rendered":"
In this tutorial, we will be building a calculator app using Angular and RxJS.<\/p>\n
Angular is a popular JavaScript framework for building web applications, and RxJS is a powerful library for reactive programming. By combining these two technologies, we can create a calculator app that updates in real-time as the user interacts with it.<\/p>\n
Before you start, make sure you have the following installed:<\/p>\n
To start, let’s create a new Angular project. Open your terminal and run the following command:<\/p>\n
ng new calculator-app\n<\/code><\/pre>\nThis will create a new Angular project with the name “calculator-app”.<\/p>\n
Navigate to your project directory:<\/p>\n
cd calculator-app\n<\/code><\/pre>\nNow, let’s generate a new component called “calculator”:<\/p>\n
ng generate component calculator\n<\/code><\/pre>\nThis command will generate a new component called “calculator” inside the “src\/app” directory.<\/p>\n
Step 2: Building the calculator UI<\/h2>\n
Open the “calculator.component.html” file and replace its contents with the following code:<\/p>\n
<div class=\"calculator\">\n <h1>Calculator<\/h1>\n <div class=\"result\">{{ result }}<\/div>\n\n <div class=\"keys\">\n <div class=\"row\">\n <button (click)=\"addNumber(7)\">7<\/button>\n <button (click)=\"addNumber(8)\">8<\/button>\n <button (click)=\"addNumber(9)\">9<\/button>\n <button (click)=\"divide()\">\/<\/button>\n <\/div>\n <div class=\"row\">\n <button (click)=\"addNumber(4)\">4<\/button>\n <button (click)=\"addNumber(5)\">5<\/button>\n <button (click)=\"addNumber(6)\">6<\/button>\n <button (click)=\"multiply()\">*<\/button>\n <\/div>\n <div class=\"row\">\n <button (click)=\"addNumber(1)\">1<\/button>\n <button (click)=\"addNumber(2)\">2<\/button>\n <button (click)=\"addNumber(3)\">3<\/button>\n <button (click)=\"subtract()\">-<\/button>\n <\/div>\n <div class=\"row\">\n <button (click)=\"addNumber(0)\">0<\/button>\n <button (click)=\"reset()\">C<\/button>\n <button (click)=\"calculate()\">=<\/button>\n <button (click)=\"add()\">+<\/button>\n <\/div>\n <\/div>\n<\/div>\n<\/code><\/pre>\nIn this HTML code, we have a div with the class “calculator” which contains the calculator UI. Inside the div, we have a header with the text “Calculator” and a div with the class “result” to display the result of the calculations.<\/p>\n
Below the header and result div, we have a div with the class “keys” which contains the calculator buttons. Each row of buttons is wrapped in a div with the class “row”. Each button has a click event attached to it that calls a corresponding method defined in the component.<\/p>\n
Step 3: Adding the calculator logic<\/h2>\n
Open the “calculator.component.ts” file and replace its contents with the following code:<\/p>\n
import { Component } from '@angular\/core';\nimport { Subject } from 'rxjs';\nimport { scan } from 'rxjs\/operators';\n\n@Component({\n selector: 'app-calculator',\n templateUrl: '.\/calculator.component.html',\n styleUrls: ['.\/calculator.component.css']\n})\nexport class CalculatorComponent {\n\n private numbers$ = new Subject<number>();\n private operations$ = new Subject<string>();\n\n result = 0;\n\n constructor() {\n this.numbers$.pipe(\n scan((acc, curr) => acc + curr, 0)\n ).subscribe(result => {\n this.result = result;\n });\n\n this.operations$.pipe(\n scan((acc, curr) => curr === \"=\" ? this.result : acc + curr, 0)\n ).subscribe(result => {\n this.result = result;\n });\n }\n\n addNumber(number: number) {\n this.numbers$.next(number);\n }\n\n add() {\n this.operations$.next(\"+\");\n }\n\n subtract() {\n this.operations$.next(\"-\");\n }\n\n multiply() {\n this.operations$.next(\"*\");\n }\n\n divide() {\n this.operations$.next(\"\/\");\n }\n\n reset() {\n this.numbers$.next(0);\n }\n\n calculate() {\n this.operations$.next(\"=\");\n }\n\n}\n<\/code><\/pre>\nIn this TypeScript code, we import the necessary modules from the RxJS library. We create two private Subjects: numbers$<\/code> for handling number inputs and operations$<\/code> for handling operator inputs.<\/p>\nInside the constructor, we use the scan<\/code> operator to perform the calculations based on the inputs received. The result of the calculations is then subscribed to and stored in the result<\/code> variable, which is displayed in the UI.<\/p>\nWe also have methods for handling number input and operator input. These methods emit the corresponding values to the Subjects, which triggers the calculations.<\/p>\n
Step 4: Styling the calculator<\/h2>\n
Open the “calculator.component.css” file and add the following code:<\/p>\n
.calculator {\n max-width: 400px;\n margin: 0 auto;\n text-align: center;\n}\n\n.result {\n font-size: 24px;\n margin-bottom: 20px;\n}\n\n.keys {\n display: grid;\n grid-template-columns: repeat(4, 1fr);\n grid-gap: 10px;\n margin-bottom: 20px;\n}\n\n.row {\n display: flex;\n justify-content: space-between;\n}\n\nbutton {\n padding: 10px;\n}\n<\/code><\/pre>\nThis CSS code adds some basic styling to the calculator UI. The calculator is centered on the page and has a maximum width of 400px. The result is styled with a larger font size, and the buttons are arranged in a grid layout.<\/p>\n
Step 5: Running the app<\/h2>\n
To run the calculator app, open your terminal and navigate to the project directory. Then, run the following command:<\/p>\n
ng serve\n<\/code><\/pre>\nThis will start the Angular development server. Open your browser and go to http:\/\/localhost:4200 to see the calculator app in action.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we have built a calculator app using Angular and RxJS. We have learned how to handle user input, perform calculations using the RxJS scan<\/code> operator, and update the UI in real-time. With the knowledge gained from this tutorial, you can now build more advanced calculator apps or explore other possibilities with Angular and RxJS.<\/p>\n","protected":false},"excerpt":{"rendered":"In this tutorial, we will be building a calculator app using Angular and RxJS. Angular is a popular JavaScript framework for building web applications, and RxJS is a powerful library for reactive programming. By combining these two technologies, we can create a calculator app that updates in real-time as the Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[360,363,357,359,362,361,358,356],"yoast_head":"\nHow to Build a Calculator App with Angular and RxJS - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n