1

在我的构造函数中,我通过:

constructor($compile, uiCalendarConfig) {
    'ngInject';

    this.$compile = $compile;
    this.uiCalendarConfig = uiCalendarConfig;
}

当我在 $onInit() 开头记录它的值时

$onInit() {
    console.log('this.$compile:', this.$compile, 'this.uiCalendarConfig:', this.uiCalendarConfig);
...
}

我得到 $compile 函数的文字代码。

但是当从内部调用 $compile

eventRender( event, element, view ) {
  element.attr({'tooltip': event.title,
    'tooltip-append-to-body': true});
  console.log('event:', event);
  console.log('edited element:', element);
  console.log('view:', view);
  console.log('this.$compile:', this.$compile);
  this.$compile(element)(this);
};

里面提到:

this.uiConfig = {
      calendar: {
        height: 450,
        editable: true,
        header:{
          left: 'title',
          center: '',
          right: 'today, prev, next'
        },
        eventClick: this.alertOnEventClick,
        eventDrop: this.alertOnDrop,
        eventResize: this.alertOnResize,
        eventRender: this.eventRender
      }
};

结果console.log('this.$compile:', this.$compile);undefinedthis.$compile 的值。

这就是我的问题,因为我不知道为什么它在那里没有定义,如果在控制器的初始化它已经是一个函数。

有人知道我可能会错过什么吗?

4

1 回答 1

1

那是因为eventRender在上下文中被调用uiConfig并且在那个上下文中没有$compile我猜!我想到的一种补救方法是存储控制器的引用并在eventRender函数内部使用它。

var self = this;

eventRender( event, element, view ) {
  element.attr({'tooltip': event.title,
    'tooltip-append-to-body': true});
  console.log('event:', event);
  console.log('edited element:', element);
  console.log('view:', view);
  console.log('this.$compile:', this.$compile);
  self.$compile(element)(this);
};

我没有测试它,也许它根本不起作用。

于 2018-11-25T17:10:09.307 回答