0

我发现这种模式非常方便,但我想知道它以后是否会咬我:

app.controller('MyController', function($scope, $stateParams, MyPageService){

   var foo = MyPageService.foo($scope);

   scope.bar = function(opts){
      return foo(opts);
   };

   $scope.baz = MyPageService.baz($scope, $stateParams);


});

MyPageService 看起来像:

app.factory('MyPageService', function(MySharedService){

  return {

      foo: function($scope){
         return function(opts){
             return MySharedService.xyz(opts.v);
          }
      },

      baz: function($scope, $stateParams){
         return function(opts){
             return MySharedService.ijk(opts.z);
          }
      }

  } 

});

这是 Angular,但同样,这也只是 JavaScript,所以这应该有效,并且在我对该模式的简短测试中一直有效。

有人可能会认为这种模式有什么“错误”吗?任何未来可能无法预料的陷阱?

我不得不说,这种模式目前运行良好。当然,这样做的动机是因为我们的控制器代码变得庞大,并且非常有必要将它们分解成碎片。这似乎是最自然的方式。

4

1 回答 1

0

就短期性能而言,这对我们来说完全适用于大型应用程序,没问题。然而,还没有检查内存泄漏,但我认为这不会是一个问题。

于 2017-04-11T03:02:47.317 回答