8

angular サービスを介してディレクティブをコンパイルしようとしていますが、残念ながら機能しません。アイデアは、ポップアップにエラーを表示することです。

$exceptionHandlerサービスを変更しました。

crm.factory('$exceptionHandler', function(popup) {
    return function(exception) {
        popup.open({message: exception});
    }
});

ポップアップサービスは次のようになります。

crm.factory('popup', function ($document) {
    return {
        open: function (data) {
            var injector = angular.element(document).injector(),
                $compile = injector.get('$compile'),
                template = angular.element('<popup></popup>');

            // var ctmp = $compile(template.contents());
            $compile(template.contents());

            $document.find('body').append(template);
        }
    };
});

そして、これが$compileサービスをハードコードするのは良い考えではないと思います(しかし、角度でこれを実現する方法は思いつきません):

$compile = injector.get('$compile')

ポップアップ ディレクティブ:

crm.directive('popup', function () {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: '/public/js/templates/common/popup.html',
        link: function() {
            console.log('link()');
        },
        controller: function () {
            console.log('ctrl()');
        }
    };
});

これを行うには他の方法があるかもしれませんか?ありがとう。

4

1 に答える 1