服务和工厂的区别

这个问题在这里已经有了答案:

  • AngularJS:服务vs提供者vs工厂30个答案

  • 两人都是单身
  • 它们在写作模式上有所不同
  • 我的人员选择是使用服务

  • 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?