1

ディレクティブでトランスクルードされたテキストにフィルターを適用しようとしていますが、これを行う最善の方法がわかりません。アイデアの作業コピーは次のリンクにありますが、コンパイル機能を使用してトランスクルージョンされたテキストを取得することで、不正行為をしているように感じます。JSFiddleを参照してください。

angular.module("MyApp").directive('highlighter', function () {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
            phrase: '@',
            text: '@'
        },
        template: '<span ng-bind-html-unsafe=" text | highlight:phrase:false "></span>',
        compile: function (elem, attr, transclude) {
            transclude(elem, function (clone) {
                // grab content and store in text attribute
                var txt = clone[0].textContent;
                attr.text = txt;
            });
        }
    };
});
4

1 に答える 1

1

他の方法はhttp://jsfiddle.net/3vknn/だと思います。

angular.module("MyApp").directive('highlighter', function () {
    return {
        restrict: 'E',
        scope: {
            phrase: '@'
        },
        controller: function($scope, $filter) {
            $scope.highlight = $filter('highlight');
        },
        link: function (scope, elem, attr) {
            scope.$watch('phrase', function(phrase) {
                var html = scope.highlight( elem.text(), phrase );
                elem.html( html );
            });
        }
    };
});
于 2013-04-26T06:01:59.260 に答える