我正在构建一个应用程序,该应用程序通过 $rootScope 上的 AJAX 调用获取日历数据。我在各种控制器中使用这个对象,并且我需要能够适当地解析它,因为其中嵌套了各种对象。我应该在哪里存储逻辑来解析它?我已经将它作为 $rootScope 函数放在根控制器中,但我觉得这是工厂的用例。但是,我似乎不能(也不觉得这样做是个好主意)从模板访问工厂方法。
目前我有以下工厂:
angular.module('services',[]).
factory('dataManipulation', function(){
return{
getPerson: function(peopleObj, userID){
//Since each user has a unique ID, this returns an array with one element,
// so to simplify the view code, there's a [0] at the end
var person= peopleObj.filter(function(element, index, array) {
if (array[index].cwid == userID) {
return true;
}
})[0];
return person;
}
};
});
我的控制器中有以下内容:
angular.module('app.controllers',[]).
controller('rootCtrl', ['$rootScope', '$http', 'dataManipulation',
function($rootScope, $http, dataManipulation) {
$rootScope.getPerson = function(peopleObj, userID){
return dataManipulation.getPerson(peopleObj, userID);
}
}
]);
它有效,但我不确定我是否遵循最佳实践。
那么,这些解析模型的函数应该在 rootScope 中还是其他地方呢?