我的应用程序依赖于多个AngularJS模块,但每个模块都必须处理类似的数据。这些模块在我的页面上的不同位置使用。
我创建了一堆控制器,它们基本上负责将数据分配给$ scope变量。我认为那里有很多“代码味道”,我对所有重复的代码都有不好的感受。但目前我不知道如何预防它。
属性documentations
和infomaterial
已解析并注入我的$routeProvider
。
处理此类案件的更好方法是什么?
angular.module('MyApp.selectorDocumentations')
.controller('SelectorDocumentationsCtrl', function ($scope, FacetService, Attributes, documentations) {
$scope.documentationsData = documentations.data;
$scope.documentations = documentations.data.hitlist.content;
$scope.currentPage = documentations.data.hitlist.page.number;
$scope.totalPages = documentations.data.hitlist.page.totalPages;
$scope.hitlistContent = documentations.data.hitlist.content;
$scope.totalElements = documentations.data.hitlist.page.totalElements;
FacetService.prepare(documentations.data);
$scope.hardReset = function () {
FacetService.hardReset(false, documentations.data);
};
$scope.manufacturer = function (item) {
return Attributes.getManufacturer(item);
};
$scope.project = function (item) {
return Attributes.getProject(item);
};
$scope.projectBookVersion = function (item) {
return Attributes.getProjectBookVersion(item);
};
});
angular.module('MyApp.selectorInfomaterial')
.controller('SelectorInfomaterialCtrl', function ($scope, FacetService, Attributes, infomaterial) {
$scope.informaterialData = infomaterial.data;
$scope.infomaterials = infomaterial.data.hitlist.content;
$scope.currentPage = infomaterial.data.hitlist.page.number;
$scope.totalPages = infomaterial.data.hitlist.page.totalPages;
$scope.hitlistContent = infomaterial.data.hitlist.content;
$scope.totalElements = infomaterial.data.hitlist.page.totalElements;
FacetService.prepare(infomaterial.data);
$scope.hardReset = function () {
FacetService.hardReset(false, infomaterial.data);
};
$scope.mediaType = function (item) {
return Attributes.getMediaType(item);
};
$scope.mediaTitle = function (item) {
return Attributes.getMediaTitle(item);
};
$scope.mediaSubline = function (item) {
return Attributes.getMediaSubline(item);
};
});
答案 0 :(得分:1)
app.controller('SelectorDocumentationsCtrl', function ($scope, documentations, MyDataHandler) {
$scope.myDataHandler = new MyDataHandler(documentations);
});
app.controller('SelectorInfomaterialCtrl', function ($scope, infomaterial, MyDataHandler) {
$scope.myDataHandler = new MyDataHandler(informaterial);
});
那些也可以在ui-router
的状态下初始化。如果您需要帮助来建立工厂/服务,请告诉我。