【发布时间】:2017-03-28 14:39:31
【问题描述】:
我用 jQuery 编写了一个日历控件,我想在 Angular 2 项目中使用它。
我从关于这个主题的其他答案中了解到,我可以使用 jQuery 的 getScript() API 调用外部 JavaScript 文件。
我的calendar.component.ts 看起来像这样:
import { Component, OnInit, AfterViewInit } from '@angular/core';
import { Auth } from '../auth.service';
declare var $:any;
declare var CustomCal:any;
@Component({
selector: 'app-calendar',
templateUrl: './calendar.component.html',
styleUrls: ['./calendar.component.css']
})
export class CalendarComponent implements OnInit {
private year : number;
myCal : any;
constructor(private auth : Auth) {
}
ngOnInit() {
}
ngAfterViewInit() {
this.year = 2017;
$.getScript('./app/calendar/zapCalendar.js', function(){
console.log("got call'd back");
this.myCal = new CustomCal(2017);
});
}
}
我收到控制台消息“已回电”,然后是一条错误消息,指出 CustomCal 未定义。
我的CustomCal 类在zapCalendar.js 中定义如下:
class CustomCal
{
constructor(nYear) {
this._mouseDown = false;
this._mouseDrag = false;
this._lastItem = 0;
this._nYear = nYear;
this.CreateCalendarFrame();
this.AddEventHandlers(this);
}
...
}
我尝试在zapCalendar.js 文件中导出类,并尝试将以下内容添加到zapCalendar.js 文件中:
$( function() {
var myCal = new CustomCal(2017);
});
我在这里错过了什么?
更新:
我刚刚替换了这个(zapCalendar.js)
$( function() {
var myCal = new CustomCal(2017);
});
用这个:
var x = new CustomCal(2017);
现在日历正在正确呈现。但我想(如果可能的话)在我的打字稿中获得对日历的引用。这可能吗?
【问题讨论】:
标签: javascript jquery angular getscript