Thursday, August 11, 2016

Calculation IN Angular JS

<script>
//Defining Services Using Service
var app = angular.module('app', []);
 
app.service('MathService', function() {
    this.add = function(a, b) { return a + b };    
    this.subtract = function(a, b) { return a - b };  
    this.multiply = function(a, b) { return a * b };
    this.divide = function(a, b) { return a / b };
});
 
app.service('CalculatorService', function(MathService){
    this.square = function(a) { return MathService.multiply(a,a); };
    this.cube = function(a) {  
        return MathService.multiply(a, MathService.multiply(a,a));  
        };
});
 
app.controller('CalculatorController', function($scope, CalculatorService) {
    $scope.doSquare = function() {
        $scope.answer = CalculatorService.square($scope.number);
    }
    $scope.doCube = function() {
        $scope.answer = CalculatorService.cube($scope.number);
    }
});
</script>

 
<div ng-app="app">
    <div ng-controller="CalculatorController">
        Enter a number:
        <input ng-model="number" type="number">
      <button ng-click="doSquare()"><b>X<sup>2</sup></b></button>
      <button ng-click="doCube()"><b>X<sup>3</sup></b></button>
         
        <div>Answer: {{answer}}</div>
    </div>
</div>  

No comments:

Post a Comment