/// <reference path="Script.js" />
myApp.factory("MathService", function ()
{
return
{
addition :function (a, b)
{
return a + b;
},
subtract :function (a, b)
{
return a - b;
}
};
});
/// <reference path="angular.js" />
var myApp = angular.module("myModule", []);
var myController = function ($scope, MathService)
{
$scope.AddFunction=function(a,b)
{
$scope.output = MathService.addition(parseFloat(a),parseFloat(b))
}
$scope.SubFunction = function (a, b)
{
$scope.output = MathService.subtract(parseFloat(a), parseFloat(b))
}
};
myApp.controller("myCont", myController);
<!DOCTYPE html>
<html ng-app="myModule">
<head>
<script src="Scripts/angular.js"></script>
<script src="Scripts/Script.js"></script>
<script src="Scripts/MathService.js"></script>
<style>
table, th, td
{
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd)
{
background-color: #ffe6e6;
}
table tr:nth-child(even)
{
background-color: #ccffcc;
}
</style>
</head>
<body style="font-family:Arial; font-size:medium; color:darkblue">
<div ng-controller="myCont">
<h3>Using custom web service in AngularJS.</h3>
<br/>
<table>
<tr>
<th>Enter First Number</th>
<td><input type="text" ng-model="a"></td>
</tr>
<tr>
<th>Enter Second Number</th>
<td><input type="text" ng-model="b"></td>
</tr>
<tr>
<th>Result</th>
<td><input type="text" ng-model="output"></td>
</tr>
<tr>
<td><input type="button" value="ADD" ng-click="AddFunction(a,b)"/>
</td>
<td><input type="button" value="SUB" ng-click="SubFunction(a,b)"/>
</td>
</tr>
</table>
</div>
</body>
</html>