【问题标题】:How to create factory in angular of an existing service如何以现有服务的角度创建工厂
【发布时间】:2016-09-22 11:39:59
【问题描述】:

我正在学习angularjs 并阅读有关服务和工厂的文章。此ARTICLE 上提供的一个示例是此Service Fiddle

在阅读本文时,我正在尝试将服务转换为工厂,这是指向 My Factory Fiddle 的链接。但它仍然没有按预期工作我不确定我做错了什么。

HTML

  <div ng-app="app">
      <div ng-controller="CalculatorController">
          Enter a number:
          <input type="number" ng-model="number" />
          <button ng-click="doSquare()">X<sup>2</sup></button>
          <button ng-click="doCube()">X<sup>3</sup></button>

          <div>Answer: {{answer}}</div>
      </div>
  </div>

JS

    var app = angular.module('app', []);

    app.factory('MathService', function() {
    var myFactory = {};
        myFactory.add = function(a, b) { return a + b };

        myFactory.subtract = function(a, b) { return a - b };

        myFactory.multiply = function(a, b) { return a * b };

        myFactory.divide = function(a, b) { return a / b };

        retutn myFactory;
    });

    app.factory('CalculatorService', function(MathService){
        var calcFactory = {};
        calcFactory.square = function(a) { return MathService.multiply(a,a); };
        calcFactory.cube = function(a) { return MathService.myFactory.multiply(a, MathService.myFactory.multiply(a,a)); };

        return calcFactory;

    });

    app.controller('CalculatorController', function($scope, CalculatorService) {

        $scope.doSquare = function() {
            $scope.answer = CalculatorService.calcFactory.square($scope.number);
        }

        $scope.doCube = function() {
            $scope.answer = CalculatorService.calcFactory.cube($scope.number);
        }
    });

【问题讨论】:

    标签: javascript jquery angularjs service factory


    【解决方案1】:

    由于某种原因,您抱怨它无法实例化“应用”模块,所以一切都失败了。

    无论如何,这里有一个工作示例:https://jsfiddle.net/gom2q6bz/1/

    请注意,您无需拨打CalculatorService.calcFactory.cube,而是拨打CalculatorService.cube

    var app = angular.module('app', []);
    
    app.factory('MathService', function() {
      var service = {};
      service.add = function(a, b) {
        return a + b
      };
    
      service.subtract = function(a, b) {
        return a - b
      };
    
      service.multiply = function(a, b) {
        return a * b
      };
    
      service.divide = function(a, b) {
        return a / b
      };
    
      return service;
    });
    
    app.factory('CalculatorService', function(MathService) {
      var service = {};
      service.square = function(a) {
        return MathService.multiply(a, a);
      };
      service.cube = function(a) {
        return MathService.multiply(a, MathService.multiply(a, a));
      };
    
      return service;
    
    });
    
    app.controller('CalculatorController', function($scope, CalculatorService) {
    
      $scope.doSquare = function() {
        $scope.answer = CalculatorService.square($scope.number);
      }
    
      $scope.doCube = function() {
        $scope.answer = CalculatorService.cube($scope.number);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-09
      • 2016-07-20
      • 2018-01-10
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      相关资源
      最近更新 更多