Angular js$parse不起作用



我使用typescript编写了一个指令。下面是我的指示。

   'use strict';
    module App.Directives {
    interface IPageModal extends ng.IDirective {
    }
    interface IPageModalScope extends ng.IScope {
    }
    class PageModal implements IPageModal {
        static directiveId: string = 'pageModal';
        restrict: string = "A";
        constructor(private $parse: ng.IParseService) {
        }
        link = (scope: IPageModalScope, element, attrs) => {
            element.click((event) => {
                event.preventDefault();
                var options = {
                    backdrop: 'static',
                    keyboard: false
                };
                event.openModal = function () {
                    $('#' + attrs['targetModal']).modal(options);
                };
                event.showModal = function () {
                    $('#' + attrs['targetModal']).modal('show');
                };
                event.closeModal = function () {
                    $('#' + attrs['targetModal']).modal('hide');
                };
                var fn = this.$parse(attrs['pageModal']);
                fn(scope, { $event: event });
            });
        }
    }
    //References angular app
    app.directive(PageModal.directiveId, ['$parse', $parse => new PageModal($parse)]);
}

在HTML 中使用

<button class="btn blue-grey-900" target-modal="emplpyeeViewModal" page-modal="vm.addEmployee($event)">
    <i class="icon-plus  m-b-xs"></i>
</button>

在控制器中的使用

addEmployee($event) {
    $event.openModal();
};

这条线路不通。CCD_ 1。我不明白出了什么问题。错误为

这个$解析未定义。服务被调用两次

这很简单:this不是类的作用域,因为function openmodal(event) {定义了自己的作用域。

在类级别上声明函数或使用箭头函数,例如

link = (scope: IPageModalScope, element, attrs) => {
    element.click((event) => {
        event.preventDefault();
        var options = {
            backdrop: 'static',
            keyboard: false
        };
        event.openModal = function () {
            $('#' + attrs['targetModal']).modal(options);
        };
        event.showModal = function () {
            $('#' + attrs['targetModal']).modal('show');
        };
        event.closeModal = function () {
            $('#' + attrs['targetModal']).modal('hide');
        };
        var fn = this.$parse(attrs['pageModal']);//does not work here
        fn(scope, { $event: event });
    });
}

最新更新