【问题标题】:Write jquery library function into typescript将 jquery 库函数写入打字稿
【发布时间】:2016-08-21 01:52:09
【问题描述】:

我用打字稿写了一个指令。代码是这样的。

'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'); //错误代码行

【问题讨论】:

    标签: javascript jquery angularjs typescript


    【解决方案1】:

    jquery bootstrap .modal 函数然后发生错误

    如果是编译时错误,您需要类型定义。更多:https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html

    修复

    foo.d.ts 中的一个快速的:

    interface JQuery {
      modal: any;
    }
    

    【讨论】:

    • 我怎样才能调用这条线 $('#' + attrs['targetModal']).modal('hide');
    • 使用jquery.d.ts 和我提供的foo.d.ts:您的代码$('#' + attrs['targetModal']).modal('hide'); 将编译得很好
    • .modal是twitter bootstrap的功能
    猜你喜欢
    • 2018-09-18
    • 2016-06-17
    • 2019-11-17
    • 2021-11-11
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    相关资源
    最近更新 更多