【发布时间】:2015-12-31 20:54:32
【问题描述】:
我在 angularjs 中有以下代码:
TimeSlotsModel.all()
.then(function (result) {
vm.data = result.data.data;
var events = [];
angular.forEach(vm.data, function(value,key) {
var eventName = value.name;
var startDate = new Date(value.startDate);
var endDate = new Date(value.endDate);
var selectedStartingTime =new Date(value.startTime * 1000 );
var selectedEndingTime = new Date(value.endTime * 1000);
//timing is not right, needs fixing
startTime = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(),selectedStartingTime.getHours(), selectedStartingTime.getUTCMinutes());
endTime = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(),selectedEndingTime.getUTCHours(), selectedEndingTime.getUTCMinutes());
// console.log(startTime);
events.push({
title: 'Event -' + eventName,
startTime: startTime,
endTime: endTime,
allDay: false
});
console.log(eventName);
console.log(events);
// console.log(value);
//value is the object!!
})
return events;
$scope.$broadcast('eventSourceChanged',$scope.eventSource);
})
}
每次 forEach 循环遍历我的对象数组 vm.data 时,控制台都会打印以下内容:
我的问题是:
1) 为什么要打印 4 个对象的详细信息?这是否意味着数组中的每个对象都包含其他 4 个对象?
2) 每个对象是否正确推送到事件[]?
3) 如果问题 2 的答案是否定的,我应该如何解决?
编辑:更新代码以使用 promise 返回事件数组:
//Calendar Controller
.controller('CalendarCtrl', function ($scope,TimeSlotsModel,$rootScope,$q) {
var vm = this;
function goToBackand() {
window.location = 'http://docs.backand.com';
}
function getAll() {
TimeSlotsModel.all()
.then(function (result) {
vm.data = result.data.data;
});
}
function clearData(){
vm.data = null;
}
function create(object) {
TimeSlotsModel.create(object)
.then(function (result) {
cancelCreate();
getAll();
});
}
function update(object) {
TimeSlotsModel.update(object.id, object)
.then(function (result) {
cancelEditing();
getAll();
});
}
function deleteObject(id) {
TimeSlotsModel.delete(id)
.then(function (result) {
cancelEditing();
getAll();
});
}
function initCreateForm() {
vm.newObject = {name: '', description: ''};
}
function setEdited(object) {
vm.edited = angular.copy(object);
vm.isEditing = true;
}
function isCurrent(id) {
return vm.edited !== null && vm.edited.id === id;
}
function cancelEditing() {
vm.edited = null;
vm.isEditing = false;
}
function cancelCreate() {
initCreateForm();
vm.isCreating = false;
}
// initialising the various methods
vm.objects = [];
vm.edited = null;
vm.isEditing = false;
vm.isCreating = false;
vm.getAll = getAll;
vm.create = create;
vm.update = update;
vm.delete = deleteObject;
vm.setEdited = setEdited;
vm.isCurrent = isCurrent;
vm.cancelEditing = cancelEditing;
vm.cancelCreate = cancelCreate;
vm.goToBackand = goToBackand;
vm.isAuthorized = false;
//rootScope refers to the universal scope, .$on is a receiver for the
//message 'authorized'
$rootScope.$on('authorized', function () {
vm.isAuthorized = true;
getAll();
});
$rootScope.$on('logout', function () {
clearData();
});
if(!vm.isAuthorized){
$rootScope.$broadcast('logout');
}
initCreateForm();
getAll();
$scope.calendar = {};
$scope.changeMode = function (mode) {
$scope.calendar.mode = mode;
};
$scope.loadEvents = function () {
$scope.calendar.eventSource = getEvents();
$scope.$broadcast('eventSourceChanged',$scope.eventSource);
};
$scope.onEventSelected = function (event) {
console.log('Event selected:' + event.startTime + '-' + event.endTime + ',' + event.title);
};
$scope.onViewTitleChanged = function (title) {
$scope.viewTitle = title;
};
$scope.today = function () {
$scope.calendar.currentDate = new Date();
};
$scope.isToday = function () {
var today = new Date(),
currentCalendarDate = new Date($scope.calendar.currentDate);
today.setHours(0, 0, 0, 0);
currentCalendarDate.setHours(0, 0, 0, 0);
return today.getTime() === currentCalendarDate.getTime();
};
$scope.onTimeSelected = function (selectedTime) {
console.log('Selected time: ' + selectedTime);
};
function getEvents(object){
var deferred = $q.defer();
TimeSlotsModel.all()
.then(function (result) {
vm.data = result.data.data;
var events = [];
angular.forEach(vm.data, function(value,key) {
var eventName = value.name;
var startDate = new Date(value.startDate);
var endDate = new Date(value.endDate);
var selectedStartingTime = new Date(value.startTime * 1000 );
var selectedEndingTime = new Date(value.endTime * 1000);
//timing is not right, needs fixing
startTime = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(),selectedStartingTime.getHours(), selectedStartingTime.getUTCMinutes());
endTime = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(),selectedEndingTime.getUTCHours(), selectedEndingTime.getUTCMinutes());
// console.log(startTime);
events.push({
title: 'Event -' + eventName,
startTime: startTime,
endTime: endTime,
allDay: false
});
// console.log(eventName);
// console.log(events);
// console.log(value);
// console.log(key);
// console.log(value);
//value is the object!!
})
deferred.resolve(events);
// return events;
})
return deferred.promise;
console.log(deferred.promise);
}
【问题讨论】:
-
您仍然从异步函数调用 (
.all().then()) 返回一个对象,但它不起作用:Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference;return ...之后的任何内容都无法访问,因此无法执行 -
警告:我没有详细阅读您的代码,但是:记录的对象旁边的那些小蓝色
is 的意思是“在展开详细信息时重新评估了对象” (将鼠标悬停在它上面以获取确切措辞的工具提示)- 即它可能在被记录后 被修改,并且您会在日志中看到更新后的对象。 -
@andreas 我已经阅读了链接,但我不确定我应该如何返回我的事件数组,考虑到詹姆斯是正确的,我的对象被正确推送并且它只是更新我的控制台记录。
-
@andreas 是的,但我使用的是 angularjs,所以我按照这里的示例而不是 javascript - getting the return data of a async function inside a function,但它仍然无法正常工作。
标签: javascript arrays angularjs ionic-framework ionic