如何使用angular fullstack从$onInit()中定位构造的数组

How to target constructed array from within $onInit() using angular-fullstack

本文关键字:定位 数组 angular 何使用 fullstack onInit      更新时间:2023-09-26

我正在进行一个使用Babel和angular 1.5.0的angular全栈项目。

问题是,当我构建一个数组(this.events=[])时,我无法将此数组定位在$onInit()上,我应该在那里填充要显示在ui日历上的数据。我在这个上面做这个。awsomeTesting=[];

因此,我需要用$onInit()中$http.get('/api/calendars/')的数据填充这个.events[]数组,但我不明白为什么我不能以数组为目标来填充数据。

我做错了什么?

这是我的代码:

'use strict';
(function() {
class MainController {
  constructor($http, $scope, socket, uiCalendarConfig) {
    this.$http = $http;
    this.socket = socket;
    this.awesomeThings = [];
    this.awesomeTesting = [];
    this.events = [];
    this.events.splice(0, this.events.length);
    this.eventSources = [this.events];
    console.log(this.eventSources);

    /* config object */
    $scope.uiConfig = {
      calendar:{
        height: 450,
        editable: true,
        header:{
          left: 'month',
          center: 'title',
          right: 'today prev,next'
        },
        dayClick: $scope.alertEventOnClick,
        eventDrop: $scope.alertOnDrop,
        eventResize: $scope.alertOnResize
      }
    };
    $scope.$on('$destroy', function() {
      socket.unsyncUpdates('thing');
    })
  }
  $onInit() {
    this.$http.get('/api/things').then(response => {
      this.awesomeThings = response.data;
      this.socket.syncUpdates('thing', this.awesomeThings);
    });
    this.$http.get('/api/calendars').then(response => {
      this.awesomeTesting = response.data;
      this.awesomeTesting.forEach(function (objectItem) {
        console.log('displays property in each object: ', objectItem.title);
        this.events.push({
          title: objectItem.title,
          start: objectItem.start
        });
      });
      this.socket.syncUpdates('calendar', this.awesomeTesting);
    });
  }
  addThing() {
    if (this.newThing) {
      this.$http.post('/api/things', { name: this.newThing });
      this.newThing = '';
    }
  }
  deleteThing(thing) {
    this.$http.delete('/api/things/' + thing._id);
  }
}
angular.module('myApp')
  .component('main', {
    templateUrl: 'app/main/main.html',
    controller: MainController
  });
})();

我通过制作for循环而不是.forEach()来解决这个问题,但我仍然想知道如何使用.forEah()来完成这个问题?

for循环的解决方案:

for (var i = 0; i < this.awesomeTesting.length; i++) {
    this.events.push({
      title: this.awesomeTesting[i].title,
      start: this.awesomeTesting[i].start
    });
  }

尝试删除this.events.splice(0, this.events.length);希望这能有所帮助。

试试这个:

var that=this;然后替换这个事件->那个事件

`$onInit() {
*var that = this;*
this.$http.get('/api/things').then(response => {
  this.awesomeThings = response.data;
  this.socket.syncUpdates('thing', this.awesomeThings);
});
this.$http.get('/api/calendars').then(response => {
  this.awesomeTesting = response.data;
  this.awesomeTesting.forEach(function (objectItem) {
    console.log('displays property in each object: ', objectItem.title);
    *that.events.*push({
      title: objectItem.title,
      start: objectItem.start
    });
  });
  this.socket.syncUpdates('calendar', this.awesomeTesting);
});

}`