Created
April 11, 2014 22:12
-
-
Save duycuong87vn/10505787 to your computer and use it in GitHub Desktop.
Depedency inject ( MathService , CaculatorService, Controller ) CaculatorService has dependency on MathService and use multiply method . MathServices is custom service : 4 method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script> | |
<meta charset="utf-8"> | |
<title>CaculatorApp with DI_Services_Controller</title> | |
</head> | |
<body> | |
<div data-ng-app="app"> | |
<div data-ng-controller="CalculatorCtrl"> | |
<input type="number" data-ng-model="number"/> | |
<button data-ng-click="doSquare()">Square</button> | |
<button data-ng-click="doCube()">Cube</button> | |
<br /> | |
<div>Answer : {{answer}}</div> | |
</div> | |
</div> | |
<script> | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myApp = angular.module('app', []); | |
myApp.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; | |
}; | |
}); | |
myApp.service('CaculatorService', function(MathService){ | |
this.square = function(a){ | |
return MathService.multiply(a, a); | |
}; | |
this.cube = function(a){ | |
return MathService.multiply(a, MathService.multiply(a, a)); | |
}; | |
}); | |
myApp.controller('CalculatorCtrl', function($scope, CaculatorService){ | |
$scope.doSquare = function(){ | |
$scope.answer = CaculatorService.square($scope.number); | |
}; | |
$scope.doCube = function(){ | |
$scope.answer = CaculatorService.cube($scope.number); | |
}; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment