如果连接到我的 API 时出现错误,有人可以帮我弄清楚如何在我的 AngularJS 工厂中返回硬编码数据。我的硬编码数据位于另一个名为“dataFactory”的工厂中。感谢您的帮助。
service.factory("ScheduleFactory", ['$http', '$resource', '$q', 'dataFactory', function($http, $resource, $q, dataFactory) {
var objFactory = {};
objFactory.getDaysOfWeek = function(includeWeekends) {
var days = [];
var API = $resource(restURL + '/daysOfWeek/');
API.query()
.$promise
.then(function(data) {
isEmpty = (data.length === 0);
if (!isEmpty) {
days = data;
};
})
.catch(function(error) {
console.log("rejected " + JSON.stringify(error));
var data = null;
days = dataFactory.daysOfWeek;
console.log(days.length); // returns 5
});
console.log("after promise " + days.length); // returns 'after promise 0'
return days;
};
我的 dataFactory 定义如下:
dataApp.factory("dataFactory", function() {
objDataFactory = {};
objDataFactory.daysOfWeek = [
{ dayName: 'Monday'
, dayAbbrev: 'Mon'
, index: 1
, isSelected: false
, isWeekday: true }
, { dayName: 'Tuesday'
, dayAbbrev: 'Tue'
, index: 2
, isSelected: false
, isWeekday: true }
, { dayName: 'Wednesday'
, dayAbbrev: 'Wed'
, index: 3
, isSelected: false
, isWeekday: true }
, { dayName: 'Thursday'
, dayAbbrev: 'Thu'
, index: 4
, isSelected: false
, isWeekday: true }
, { dayName: 'Friday'
, dayAbbrev: 'Fri'
, index: 5
, isSelected: false
, isWeekday: true }
];
return objDataFactory;
});