当我运行此代码并且只想获取列表中的所有项目时,控制台会告诉我“无法读取null的属性'readOrganizations'。这是我第一次使用工厂,所以请帮忙!
代码如下:
app.factory("organizationsService", ["$rootScope", function($rootScope) {
var svc = {};
var data = null;
var siteURL = "dthswdththrth";
//CRUD
svc.createOrganization = function(organization) {
//REST call to POST data for SP
data.push(organization);
}
svc.readOrganizations = function() {
$http({
method: 'GET',
url: siteURL + '/_api/web/Lists/getbytitle(\'Organizations\')/Items',
headers: {
'Accept': 'application/json; odata=verbose'
}
}).success(function(d) {
$(d.d.results).each(function(i, e) {
data.push({
id: e['Id'],
title: e['Title']
});
});
callback(data);
}).error(function(er) {
alert(er);
});
}
svc.updateOrganization = function(index, organization) {
//REST call to MERGE data for SP
data[index] = organization;
}
svc.deleteOrganization = function(index) {
//REST call to DELETE data for SP
}
return data;
}]);
app.controller("organizationsController", ["$scope", "$location", "$routeParams", "organizationsService",
function($scope, $location, $routeParams, organizationsService) {
$scope.data = organizationsService.readOrganizations();
$scope.addOrganization = function() {
$location.path("/organizations/add");
};
$scope.editOrganization = function(index) {
$location.path("/organizations/" + index);
};
}
]);
工厂/服务是否完全必要?感谢您的帮助!