我是AngularJS中的新成員,我正在做一個AngularJS應用程序的重構,並且我注意到有一個控制器文件包含許多操作和設置作用域變量的函數。AngularJS - 在多個文件中拆分控制器函數
下面的例子:
test.controller('testCtrl', function testCtrl($scope) {
$scope.init_filters = function() {
$scope.filter_1 = [];
$scope.filter_2 = [];
$scope.filter_3 = [];
$scope.filter_4 = [];
$scope.filter_5 = [];
};
$scope.showChanges = function() {
if ($scope.item_list.length > 0) {
$scope.messages = [];
for (var i = 0; i < $scope.item_list.length; i++) {
$scope.messages.push($scope.item_list[i].message);
}
$scope.another_function();
}else{
// other stuff
}
};
//other functions like these
}
所以,我想在多個JS文件分割這些功能。我搜索了這個問題,並發現在很多情況下使用了一項服務。但我認爲這不是我的情況,因爲我需要直接處理控制器的範圍。
我的意思是,我不想要一個分離的函數,作爲參數的一些範圍變量並返回變量。
那麼,做這種事情的最佳做法是什麼?可能嗎?
如果你想遵循模塊化的方法。咖啡劇本與咕嚕聲允許做到這一點。請看看這個包'https:// github.com/gruntjs/grunt-contrib-coffee' –