服务和工厂的区别
这个问题在这里已经有了答案:
var app = angular.module('app', []);
app.factory('MathService', function(){
return {
multiply : function(a, b){
return a*b;
}
}
});
app.factory('CalculateResult', ['MathService', function(MathService){
return {
square : function(a){
return MathService.multiply(a,a);
},
cube : function(a){
return MathService.multiply(a, MathService.multiply(a,a));
}
}
}]);
app.controller('CalculatorController', ['CalculateResult', '$scope', function(CalculateResult, $scope){
$scope.doSquare = function(){
$scope.answer = CalculateResult.square($scope.number);
};
$scope.doCube = function(){
$scope.answer = CalculateResult.cube($scope.number);
}
}]);
这是答案感谢支持
链接地址: http://www.djcxy.com/p/13611.html上一篇: Difference between service and factory
下一篇: Why should I choose using a Service over a Factory in AngularJS?