我试图做一个日历,在AngularJS的请求resto中可以获取信息。我想创建一个日历,您可以在其中注册事件。使用json属性的日期,它可以输入此事件并在当天注册一个图标。

我的错误是,如您在图片中看到的,它将获取一个数组并插入所有字段,而不仅是在注册到json的那一天。我的工作重复了42次,这是日历上的平方数。
没有人知道我的方法的错误之处是我不理解。

图片

javascript - 在json/angularjs中重复数组-LMLPHP

控制者

$scope.setDayContent = function(date) {

    return $http ({
        method : "GET",
        url : "app/components/home/controller/test_calendar.json"
      }).then(function mySucces(response) {
           return response.data.data[0].title;
        }, function myError(response) {
          $scope.valor = response.statusText;
      });

};


json

{"data":[
    {"title":"John",
     "start":"2016-10-22"
    }, {
     "title":"Richard",
     "start":"2016-10-25"
    }
]}


html

<calendar-md flex layout layout-fill
  calendar-direction="direction"
  on-prev-month="prevMonth"
  on-next-month="nextMonth"
  on-day-click="dayClick"
  ng-model='selectedDate'
  week-starts-on="firstDayOfWeek"
  tooltips="tooltips"
  day-format="dayFormat"
  day-content="setDayContent"
  ></calendar-md>

最佳答案

函数setDateContent在日历中的每个日期运行。在success回调中,您需要添加逻辑以检查返回的数据,并设置变量或返回当天所需的信息。

您不应该每天加载JSON。您应该加载JSON,然后在函数中检查每个日期。

    $scope.loadData = function(){
      return $http ({
        method : "GET",
        url : "app/components/home/controller/test_calendar.json"
      }).then(function mySucces(response) {
           $scope.jsonData = response.data;
        }, function myError(response) {
          $scope.valor = response.statusText;
      });
    }

$scope.setDayContent = function(date) {

    //check if $scope.jsonData contains date parameter
    // return title

};

关于javascript - 在json/angularjs中重复数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40308184/

10-12 12:34
查看更多