Thursday 12 October 2023

Angularjs-Write A Addition ,Subtraction Program In Angularjs Using Custom Service

 

Write A Addition ,Subtraction Program In Angularjs Using Custom Service

 

<html ng-app="mathApp">

<head>

    <title>AngularJS Addition and Subtraction</title>

    <script src="angular.min.js"></script>

</head>

<body ng-controller="MathController">

    <h1>Addition and Subtraction</h1>

    <input type="number" ng-model="number1" placeholder="Enter number 1">

    <input type="number" ng-model="number2" placeholder="Enter number 2">

    <button ng-click="add()">Add</button>

    <button ng-click="subtract()">Subtract</button>

    <p>Result: {{ result }}</p>

<script>

angular.module('mathApp', [])

    .service('mathService', function() {

        this.add = function(a, b) {

            return a + b;

        };

 

        this.subtract = function(a, b) {

            return a - b;

        };

    })

    .controller('MathController', function($scope, mathService) {

        $scope.number1 = 0;

        $scope.number2 = 0;

        $scope.result = 0;

 

        $scope.add = function() {

            $scope.result = mathService.add($scope.number1, $scope.number2);

        };

 

        $scope.subtract = function() {

            $scope.result = mathService.subtract($scope.number1, $scope.number2);

        };

    });

</script>

</body>

</html>