我发现这种模式非常方便,但我想知道它以后是否会咬我:
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,所以这应该有效,并且在我对该模式的简短测试中一直有效。
有人可能会认为这种模式有什么“错误”吗?任何未来可能无法预料的陷阱?
我不得不说,这种模式目前运行良好。当然,这样做的动机是因为我们的控制器代码变得庞大,并且非常有必要将它们分解成碎片。这似乎是最自然的方式。