<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>
//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>
Very useful article! To support Angular Web Development I would also like to share that 968,139 live websites are using Angular JS. Constantly, Top Angular Web Development Company are growing rapidly with the increasing demands of Angular Web development.
ReplyDeleteGood one
ReplyDelete