1
我正在向vm.events对象“推送”一个新事件,并且一直收到TypeError: Cannot read property 'push' of undefined
错误。TypeError:无法读取未定义的属性'push' - AngularJS
我花了几个小时,我没有看到什么是未定义的确切,每个元素似乎有价值。
这是我在控制台中看到当我登录eventdata
:
Object
actions: Array[2]
color: Object
draggable: true
endsAt: Fri Dec 16 2016 08:00:00 GMT-0500 (Eastern Standard Time)
eventid: 457
resizable: true
startsAt: Fri Dec 16 2016 07:00:00 GMT-0500 (Eastern Standard Time)
title: "Asa Kris - Chris Brown "
__proto__: Object
JS(见vm.eventSaved功能)
var actions = [{
label: '<i class=\'glyphicon glyphicon-pencil\'></i>',
onClick: function(args) {
showEventModal('Edited', args.calendarEvent);
//alert.show('Edited', args.calendarEvent);
}
}, {
label: '<i class=\'glyphicon glyphicon-remove\'></i>',
onClick: function(args) {
vm.eventDeleted (args.calendarEvent)
//alert.show('Deleted', args.calendarEvent);
}
}
vm.events = [
{
title: 'An event',
color: calendarConfig.colorTypes.warning,
startsAt: moment().startOf('week').subtract(2, 'days').add(8, 'hours').toDate(),
endsAt: moment().startOf('week').add(1, 'week').add(9, 'hours').toDate(),
draggable: true,
resizable: true,
actions: actions
}, {
title: 'This is a really long event title that occurs on every year',
color: calendarConfig.colorTypes.important,
startsAt: moment().startOf('day').add(7, 'hours').toDate(),
endsAt: moment().startOf('day').add(19, 'hours').toDate(),
recursOn: 'year',
draggable: true,
resizable: true,
actions: actions
}
];
vm.eventSaved = function() {
var eventdata = {eventid : null,
title : newAppointmentCustomerId,
color : calendarConfig.colorTypes.info,
startsAt : new Date(vm.dtStart),
endsAt : new Date(vm.dtEnd),
draggable : true,
resizable : true,
actions : actions
};
var urlapieventspost = $location.protocol() + "://" + $location.host() + "/api/events";
$http.post(urlapieventspost, indata).success(function(data, status){
eventdata.eventid = data.id;
eventdata.title = data.eventtitle;
console.log(eventdata);
vm.events.push(eventdata);
}).error(function(err){
/* do something with errors */
});
};
您应该记录'vm.events',而不是'eventdata'。你可能会发现它是'vm.events'是未定义的,因为你不在不同的范围内。 –
呵呵,这很有道理。让我检查一下。谢谢 – user3489502
现在有效,事件没有传递给eventSaved函数,谢谢! – user3489502