1

如何引用工厂内部的函数?这是示例,我想在返回结果function2时使用function1(它确实失败了):

angular.module('myapp').
  factory('ExampleFactory', function ($http, $rootScope) {
  return {
    function1: function (a,b) {
      return a + b;
    },
    function2: function (a,b,c) {
      return this.function1(a,b) * c
    },    
  }
})
4

2 回答 2

0

这是使用“显示模块”设计模式的一种选择:

angular.module('myapp').
  factory('ExampleFactory', function ($http, $rootScope) {
  function function1 (a,b) {
      return a + b;
  }
  function function2 (a,b,c) {
      return function1(a,b) * c;
  }
  return {
      function1: function1,
      function2: function2,    
  }
});
于 2014-06-19T17:27:03.163 回答
0
    angular.module('myapp').
      factory('ExampleFactory', function ($http, $rootScope) {
        // define what ever function you want , outside of the return property
        function plus(a,b){
          return a + b;
        }

      return {
        function2: function (a,b,c) {
          // Now u can use that function here :
          return plus(a,b) * c
        }
      }
    })
于 2014-06-19T18:18:52.980 回答