ng-bind-html - 通过过滤器获取图像

ng-bind-html - get image via filter

我目前正在开发一个模块,用户可以在其中将文本输入到文本区域,以及一些具有以下格式的图像标签:

ii[5ae71206|100|100]ii.

这是我显示输入文本的方式:

<span ng-bind-html="localeCache[item.sysName][editLocale]['text1'] | imageFilter"></span>

"imageFilter"-过滤器应该用 <img> 替换文本中的自定义标签,因此 ii[5ae71206|100|100]ii 变为:

<img src="path-to-file-with-image-id-5ae71206" 
     style="max-width:100px;max-height:100px;">

我的过滤器源代码如下:

define(
    ['angularAMD', 'docService']
    , function(angularAMD){
        angularAMD.filter('imageFilter', function($rootScope, DocAdminService){
            return function(text) {

                var regExImgTag = /ii\[(.*?)]ii/g;
                var regExImg = /ii\[.*?\]ii/;
                var imageTags = regExImgTag.exec(text);

                if(imageTags !== null){
                    var tag = imageTags[1];
                    var parts = tag.split('|');
                    var imgTag = parts[0];
                    var width = parts[1];
                    var height = parts[2];

                   var imgString = '<img ng-src="' + $rootScope.path_to_REST_Service + imgTag + '" style="max-width:' + width + 'px; max-height:' + height + 'px;">';
                   var textNew = text.replace(regExImg, imgString);
                   console.log(textNew);
                    return (textNew);
                    });
                }
                else{
                    return text;
                }
            };
        });
    }
);

过滤器 return 正确的字符串,但视图未呈现图像。当我只是输入一些没有我的自定义图像标签的文本时,一切都按预期工作。 有什么想法吗?

只需使用简单的 src 属性,因为 ng-src 将需要再次 $compile HTML。这是一个类似的工作示例。

var app = angular.module("app", ["ngSanitize"]);
app.controller("MainController", function($scope){

  $scope.localeCache = "Some text";

});

app.filter("imageFilter", function(){
    return function (input) {
    
        //Here you add modifications to the input.
        //This is just an example
        
        var width = "100px",
            imgPath = 'http://via.placeholder.com/350x150',
            height = "100px";
        
        return '<img src="' 
        + imgPath
        + '" style="max-width:'
        + width 
        + 'px; max-height:'
        + height 
        + 'px;">';
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.5.1/angular-sanitize.min.js"></script>

<div ng-app="app" ng-controller="MainController">
  <span ng-bind-html="localeCache | imageFilter"> </span
</div>

我用适用于我的过滤器编写了简单的演示:

<div ng-bind-html="path | imageFilter" ></div>

JS

app.filter('imageFilter', function($sce){
            return function(text) {
              var html = $sce.trustAsHtml('<img src="' + text  +'" style="max-width:100px;max-height:100px;"/>'); 
               return html;
            };
        });

app.controller('FirstCtrl', function($scope, $sce) {
  $scope.path = 'https://media.mnn.com/assets/images/2014/09/running-with-dog.jpg.653x0_q80_crop-smart.jpg';
});

Demo Plunker

过滤逻辑内部有问题

好的,我想我找到了问题所在:我的过滤器中确实有一个 REST 服务调用,我在最初的 post 中删除了它,因为我认为它不重要而且只会造成混淆人:

define(
    ['angularAMD', 'docService']
    , function(angularAMD){

        angularAMD.filter('imageFilter', function($rootScope, $sce, DocService){
            return function(text) {

                var regExImgTag = /ii\[(.*?)]ii/g;
                var regExImg = /ii\[.*?]ii/;
                var imageTags = regExImgTag.exec(text);

                if(imageTags !== null){
                    var tag = imageTags[1];
                    var parts = tag.split('|');
                    var imgTag = parts[0];
                    var width = parts[1];
                    var height = parts[2];

                    DocService.listDocsByParameters({ firstParam: 'textPropertyC/s/eq/' + btoa(imgTag) }, function(response){
                        var imgId = response[0].id;

                        var imgString = '<img src="' + $rootScope.restUrl + 'doc-rest/documentById/' + imgId + '" style="max-width:' + width + 'px; max-height:' + height + 'px;">';
                        var textNew = text.replace(regExImg, imgString);
                        console.log(textNew);
                        return textNew; //This is probably the problem.
                    });
                }
                else{
                    return text;
                }
            };
        });
    }
);

我通过向我的控制器添加另一个数组解决了这个问题,它将图像标签映射到图像 id,我需要它从 REST 服务获取正确的图像。该数组将传递给过滤器:

<span ng-bind-html="localeCache[item.sysName][locale]['text1'] | imageFilter:imageMap"></span>

过滤器看起来像这样:

define(
    ['angularAMD']
    , function(angularAMD){

        angularAMD.filter('imageFilter', function($rootScope){
            return function(text, map) {

                if(typeof map === 'undefined' || typeof text === 'undefined' || text === '' || text === null){
                    return text;
                }

                var regExImgTag = /ii\[(.*?)]ii/g;
                var regExImg = /ii\[.*?]ii/;
                var imageTags = regExImgTag.exec(text);

                if(imageTags !== null){
                    var tag = imageTags[1];
                    var parts = tag.split('|');
                    var imgTag = parts[0];
                    var width = parts[1];
                    var height = parts[2];

                    var imgId = map[imgTag];

                    var imgString = '<img src="' + $rootScope.restUrl + 'doc-rest/documentById/' + imgId + '" style="max-width:' + width + 'px; max-height:' + height + 'px;">';
                    var textNew = text.replace(regExImg, imgString);
                    console.log(textNew);
                    return textNew;
                }
                else{
                    return text;
                }
            };
        });
    }
);