Angularjs不断给出TypeError:无法读取未定义的属性'dhx_security'

时间:2014-01-12 00:58:32

标签: php mysql angularjs

我正在尝试将dhtmlx事件日历用于我的angularjs webapp。我仍然是这一切的新手,并将日历与angularjs和php + mysql(从db获取事件)结合起来,在这些组合中找不到太多信息。

此代码初始化日历:

    myAppProfile.directive('dhxScheduler', function() {
  return {
    restrict: 'A',
    scope: false,
    transclude: true,
    template:'<div class="dhx_cal_navline" ng-transclude></div><div class="dhx_cal_header"></div><div class="dhx_cal_data"></div>',

    link:function ($scope, $element, $attrs, $controller){
      //default state of the scheduler
      if (!$scope.scheduler)
        $scope.scheduler = {};
      $scope.scheduler.mode = $scope.scheduler.mode || "month";
      $scope.scheduler.date = $scope.scheduler.date || new Date();

      //watch data collection, reload on changes
      $scope.$watch($attrs.data, function(collection){
        scheduler.clearAll();
        scheduler.parse(collection, "json"); <-- UNDEFINED
      }, true);

      //watch mode and date
      $scope.$watch(function(){
        return $scope.scheduler.mode + $scope.scheduler.date.toString();
      }, function(nv, ov) {
        var mode = scheduler.getState();
        if (nv.date != mode.date || nv.mode != mode.mode)
          scheduler.setCurrentView($scope.scheduler.date, $scope.scheduler.mode);
      }, true);

      //size of scheduler
      $scope.$watch(function() {
        return $element[0].offsetWidth + "." + $element[0].offsetHeight;
      }, function() {
        scheduler.setCurrentView();
      });

      //styling for dhtmlx scheduler
      $element.addClass("dhx_cal_container");

      //init scheduler
      scheduler.init($element[0], new Date(), "month");
      scheduler.load("agendaController.php");
    }
  }
});

我把箭放在那里,它一直不亮。不知道为什么会这样,我最后加载了正确的json数据,但即使日历是空的,它仍然可以工作。我不确定我做错了什么..

谢谢!

1 个答案:

答案 0 :(得分:3)

我怀疑$ watch会在初始化之后立即触发事件,然后才会将值分配给数据属性。

试试这个:

$scope.$watch($attrs.data, function(collection){
  if(collection) {
    scheduler.clearAll();
    scheduler.parse(collection, "json");
  }
}, true);