我使用 typescript 编写了一条指令。代码如下所示。
'use strict';
module App.Directives {
interface IPageModal extends ng.IDirective {
}
interface IPageModalScope extends ng.IScope {
//modal: any;
}
class PageModal implements IPageModal {
static directiveId: string = 'pageModal';
restrict: string = "A";
constructor(private $parse: IPageModalScope) {
}
link = (scope: IPageModalScope, element, attrs) => {
element.on('click', function (event) {
event.preventDefault();
var options = {
backdrop: 'static',
keyboard: false
};
event.openModal = function () {
$('#' + attrs['targetModal']).modal(options);//error
};
event.showModal = function () {
$('#' + attrs['targetModal']).modal('show');//error
};
event.closeModal = function () {
$('#' + attrs['targetModal']).modal('hide');//error
};
var fn = this.$parse(attrs['pageModal']);
fn(scope, { $event: event });
});
}
}
//References angular app
app.directive(PageModal.directiveId, ['$parse', ($parse) => new PageModal($parse)]);
}
当我调用 jquery bootstrap .modal 函数时发生错误。我如何调用下面这个函数
$('#' + attrs['targetModal']).modal('hide'); //error line of code
最佳答案
jquery bootstrap .modal function then error occurring
如果是编译时错误,则需要类型定义。更多:https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html
修复
foo.d.ts
中的快速操作:
interface JQuery {
modal: any;
}
关于javascript - 将jquery库函数写入typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878416/