javascript - How to create factory in angular of an existing service -


i learning angularjs , reading articles on service , factory. 1 example provided on article service fiddle.

while reading trying convert service factory, link my factory fiddle. still it's not working expected not sure doing wrong.

html

  <div ng-app="app">       <div ng-controller="calculatorcontroller">           enter 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>   </div> 

js

    var app = angular.module('app', []);      app.factory('mathservice', function() {     var myfactory = {};         myfactory.add = function(a, b) { return + b };          myfactory.subtract = function(a, b) { return - b };          myfactory.multiply = function(a, b) { return * b };          myfactory.divide = function(a, b) { return / b };          retutn myfactory;     });      app.factory('calculatorservice', function(mathservice){         var calcfactory = {};         calcfactory.square = function(a) { return mathservice.multiply(a,a); };         calcfactory.cube = function(a) { return mathservice.myfactory.multiply(a, mathservice.myfactory.multiply(a,a)); };          return calcfactory;      });      app.controller('calculatorcontroller', function($scope, calculatorservice) {          $scope.dosquare = function() {             $scope.answer = calculatorservice.calcfactory.square($scope.number);         }          $scope.docube = function() {             $scope.answer = calculatorservice.calcfactory.cube($scope.number);         }     }); 

for reason fiddle complain can't instantiate 'app' module, fails.

anyway, here fiddle working example: https://jsfiddle.net/gom2q6bz/1/.

please note don't need call calculatorservice.calcfactory.cube rather calculatorservice.cube;

var app = angular.module('app', []);  app.factory('mathservice', function() {   var service = {};   service.add = function(a, b) {     return + b   };    service.subtract = function(a, b) {     return - b   };    service.multiply = function(a, b) {     return * b   };    service.divide = function(a, b) {     return / b   };    return service; });  app.factory('calculatorservice', function(mathservice) {   var service = {};   service.square = function(a) {     return mathservice.multiply(a, a);   };   service.cube = function(a) {     return mathservice.multiply(a, mathservice.multiply(a, a));   };    return service;  });  app.controller('calculatorcontroller', function($scope, calculatorservice) {    $scope.dosquare = function() {     $scope.answer = calculatorservice.square($scope.number);   }    $scope.docube = function() {     $scope.answer = calculatorservice.cube($scope.number);   } }); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -