JSFiddle - React, Tailwind, and code Playground
by Sunny SM
HTML
<h1>NG SERVICE AND FACTORY APP DEMO</h1>
<div ng-app="sun" ng-controller="cal">
<p>
Enter Number : <input type="text" ng-model="num" />
<button ng-click="mul()">Multiply</button>
</p>
<p>
Result is : {{result}}
</p>
</div>
JavaScript
var obj = angular.module("sun", []);
obj.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
});
obj.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
obj.controller('cal', function($scope, CalcService) {
$scope.mul = function() {
$scope.result = CalcService.square($scope.num);
}
});