7

How can we transform/changes below code to factory rather than service

What is the better way to implement within these two, factory and service, please suggest. I'm new to AngularJs so please help me out in this concern

<html>

    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

    </head>

<body ng-app="app">


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

        <div>Answer: {{answer}}</div>
    </div>



    <script>

        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>

</body>
</html>
manish kumar
  • 4,412
  • 4
  • 34
  • 51
G2 Jakhmola
  • 796
  • 1
  • 5
  • 15
  • [Service VS provider VS factory in AngularJS](http://stackoverflow.com/questions/15666048/angularjs-service-vs-provider-vs-factory) – Mistalis Feb 21 '17 at 09:25
  • Check this link it will clear all your confusions- http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory – Sumit Sinha Feb 21 '17 at 09:41
  • To my knowledge this one is the best. Please refer. http://stackoverflow.com/questions/23074875/angularjs-factory-and-service – Wintergreen Feb 21 '17 at 09:54

2 Answers2

3
  • Both are singleton
  • They differ in terms of writing pattern
  • My personnel choice is to use service
ZAiD Chauhan
  • 141
  • 4
0
var app = angular.module('app', []);
                app.factory('MathService', function(){
                    return {
                        multiply : function(a, b){
                            return a*b;
                        }
                    }
                });

                app.factory('CalculateResult', ['MathService', function(MathService){
                        return {
                            square : function(a){
                                return MathService.multiply(a,a);
                            },

                            cube : function(a){
                                return MathService.multiply(a, MathService.multiply(a,a));
                            }
                        }
                }]);

                app.controller('CalculatorController', ['CalculateResult', '$scope', function(CalculateResult, $scope){

                    $scope.doSquare = function(){
                        $scope.answer = CalculateResult.square($scope.number);
                    };

                    $scope.doCube = function(){
                        $scope.answer = CalculateResult.cube($scope.number);
                    }

                }]);

Here is the answer thanks for supports

G2 Jakhmola
  • 796
  • 1
  • 5
  • 15