0

我正在尝试将我的对象(事件)的属性从后端保存到日历的新变量中。这是我的代码:

编辑:

function getEvents(object){

        TimeSlotsModel.all()
            .then(function (result) {
                vm.data = result.data.data;
                 angular.forEach(vm.data, function(value,key) {

        angular.forEach(value, function(value,key) {


            var id = value;
            var name =  value;
            var location = value;
            var allDay = value;
            var startDay = value;
            var endDay = value;
            var startTime = value;
            var endDay = value;
            var user = value;
            console.log(id);
            })
            })
            })
         }

它在控制台中有以下输出:

controllers.js:159 Object {id: "20", descriptives: Object, dates: Object}
controllers.js:159 20
controllers.js:159 Financial Management
controllers.js:159 
controllers.js:159 SR22
controllers.js:159 null
controllers.js:159 2015-12-30T16:00:00.000Z
controllers.js:159 59400
controllers.js:159 66600
controllers.js:159 
controllers.js:159 Object {id: "21", descriptives: Object, dates: Object}
controllers.js:159 21
controllers.js:159 Accounting II
controllers.js:159 
controllers.js:159 SR 2
controllers.js:159 null
controllers.js:159 2015-12-29T16:00:00.000Z
controllers.js:159 2015-12-30T16:00:00.000Z
controllers.js:159 14400

我应该怎么做才能将这些单独的字段保存到我的新变量中?

4

1 回答 1

2

如果您有各自的名称作为 JSON 中的键:

function getEvents(object){

    TimeSlotsModel.all()
        .then(function (result) {
            vm.data = result.data.data;
        })
 }

然后,您可以通过调用 vm.data[0].id、vm.data[0].name、vm.data[0].location 来获取这些数据(以测试该理论)进行测试。如果您在 ng-repeat 中有这些:

<div ng-repeat="event in events track by event.id">
  <span>{{event.id}}</span>
  <span>{{event.name}}</span>
  <span>{{event.location}}</span>
  ......
</div>

---- 如果您没有适当的名称作为 JSON 中的键返回:

function getEvents(object){
    var singleEvent = {};
    var events = {}
    TimeSlotsModel.all()
      .then(function (result) {
        vm.data = result.data.data;
        angular.forEach(vm.data, function(value,key) {

          angular.forEach(value, function(value,key) {

            singleEvent.id = value;
            singleEvent.name =  value;
            singleEvent.location = value;
            singleEvent.allDay = value;
            singleEvent.startDay = value;
            singleEvent.endDay = value;
            singleEvent.startTime = value;
            singleEvent.endDay = value;
            singleEvent.user = value;
            console.log(singleEvent.id);
         })
         events += singleEvent;
         singleEvent = {};
      })
    })
}
于 2015-12-30T15:49:48.870 回答