[code]

mainApp2.factory(‘MathService’, function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
};
return factory;
});

mainApp2.service(‘CalcService’, function(MathServic){
this.square = function(a) {
return MathService.multiply(a,a);
};
});

mainApp2.controller(‘CalcController’, function($scope, CalcService) {
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
};
});

[/code]

[code]

<div ng-app=”mainApp2″ ng-controller=”CalcController”>
<p>Enter a number: <input type=”number” ng-model=”number” />
<button ng-click=”square()”>X<sup>2</sup></button>
<p>Result: {{result}}</p>
<a ng-click=”alert()”>Click</a
</div>

[/code]

Code snippets from TutorialsPoint

Leave a Reply