TypeError:无法读取属性' push'未定义的 - AngularJS

时间:2016-12-16 19:52:11

标签: javascript angularjs

我" m"推动" vm.events对象的新事件,我不断收到TypeError: Cannot read property 'push' of undefined错误。

我在这上花了几个小时,我不确切地看到什么是未定义的,每个元素似乎都有价值。

这是我在登录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 */
          });
   };

1 个答案:

答案 0 :(得分:0)

vm.events未定义。因为它是唯一被称为push的对象。尝试调试并记录vm.events。如果你展示了声明vm的位置,也会有所帮助。

相关问题