angularjs自定义指令不能与ng-bind-html指令一起使用

Angularjs-read more custom directive not work with ng-bind-html directive

本文关键字:指令 一起 ng-bind-html 自定义 不能 angularjs      更新时间:2023-09-26

这些是我的自定义指令read more和to_trusted(用于转换为html)。

psp.directive('hmRead', function () {
    return {
        restrict:'AE',
        scope:{
            hmtext : '@',
            hmlimit : '@',
            hmfulltext:'@',
            hmMoreText:'@',
            hmLessText:'@',
            hmMoreClass:'@',
            hmLessClass:'@'
        },
        templateUrl: 'partials/common/read-more.html',
        controller : function($scope){
              $scope.toggleValue=function(){
                    if($scope.hmfulltext == true)
                        $scope.hmfulltext=false;
                    else if($scope.hmfulltext == false)
                        $scope.hmfulltext=true;
                    else
                        $scope.hmfulltext=true;
              }        
        }
    };
});
psp.filter('to_trusted', ['$sce', function($sce){
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }]);

在html中调用。

 <hm-read hmtext="{{data.content}}" ng-bind-html="data.content| to_trusted" hmlimit="100" hm-more-text="read more" hm-less-text="read less"></hm-read>

如果我删除ng-bind-html,那么readmore工作正常,但ng-bind-html指令readmore指令不工作。

您的指令的逻辑有点不清楚作为Read-More指令,特别是在您的指令中使用templateUrlhmfulltext参数和ng-bind-html

顺便说一下,我在你的指令中使用了假模板的指令:

template: '<p>template that is set in your directive</p>',

和假内容范围变量如下:

$scope.data={};
$scope.data.content = '<p>template that passed into directive</p>';

通过这个假模板你的指令工作正常

在线执行

显示问题在'partials/common/read-more.html'模板或数据中。content范围变量。您可以在这些地方查找错误。