【问题标题】:dhtmlx inside angular2 componentangular2组件内的dhtmlx
【发布时间】:2018-04-24 18:08:19
【问题描述】:

我想在我的 Angular2 组件中使用dhtmlx onEmptyClick 事件,我想在其中调用我的组件的函数changewall(),但出现错误:

this.changeWall 不是函数

如果我将changewall() 放在scheduler.attachEvent 之外,身体工作正常。为什么会这样?

不起作用:

 scheduler.attachEvent("onEmptyClick", function (date, e ){
                        console.log('clickBefore');
                        this.changeWall(1511305200000);
                        console.log('clickAfter');
                    });

这可行:

 this.changeWall(1511305200000);
     scheduler.attachEvent("onEmptyClick", function (date, e ){
                                console.log('clickBefore');
                                console.log('clickAfter');
                            });

完整代码:

import {Component, OnInit, AfterViewInit, HostListener} from '@angular/core';
import {WallService} from "../../services/wall.service";
declare var scheduler:any; //hide errors
@Component({
    moduleId: module.id,
    selector: 'big-calendar',
    templateUrl: 'calendar2.component.html',
    styleUrls: ['calendar2.component.scss']
})
export class Calendar2Component implements AfterViewInit {
    private wallInstance: any;
    events: any;
    constructor(private _wall: WallService) {

    }

    @HostListener('click', ['$event'])
    onClick(e) {
    // this.refreshCalendar();
    }

    refreshCalendar(){
        let date = new Date(scheduler.getState().date);
        let month = date.getMonth() + 1;
        let year = date.getFullYear();
        // scheduler.clearAll();

        scheduler.attachEvent("onEmptyClick", function (date, e ){
            console.log('clickBefore');
            this.changeWall(1511305200000);
            console.log('clickAfter');
        });

        this._wall.getCalendarEvents(month, year).subscribe((data: any)=>{
            console.log(data);
            let i = 0, events = [];

            while(i < data.events.length){
                events.push({
                    holder: "t", //userdata
                    room:   "1" ,
                    text: data.events[i].name,
                    start_date: data.events[i].start,
                    end_date: data.events[i].end +1
                });

                i++;
            }
            console.log(events);
            scheduler.init('scheduler_here',new Date(),"month");

            scheduler.parse(events,"json");
        });
    }


    ngAfterViewInit(): void {
        scheduler.config.touch = "force";
         this.refreshCalendar();
        // scheduler.parse([
        //     {id:1, text:"Meeting",   start_date:"04/11/2016 14:00",end_date:"14/12/2016 17:00"}
        // ],"json");
    }

        changeWall(timestamp){
        // let timestamp = new Date(date).getTime();
        // console.log(timestamp);
        this.wallInstance = this._wall.getInstance();
        this.wallInstance.timeChanger(timestamp);
    }
}

【问题讨论】:

    标签: javascript angular dhtmlx


    【解决方案1】:
    scheduler.attachEvent("onEmptyClick", function (date, e ){
    

    应该是

    scheduler.attachEvent("onEmptyClick", (date, e ) => {
    

    同时更改你使用function的其他地方

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    【讨论】:

    • 天哪!非常感谢
    • 如果这回答了您的问题,请考虑通过单击 up/downvote 按钮下方的复选标记来接受它,以明确问题已得到回答。谢谢。
    猜你喜欢
    • 2018-07-11
    • 2017-01-10
    • 2015-11-28
    • 2016-11-22
    • 1970-01-01
    • 2016-07-29
    • 2016-06-26
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多