简体   繁体   English

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

[英]ng-bind-html - get image via filter

I'm currently working on a module where users can enter text into a text-area, along with some image-tags which have the following format: 我目前正在开发一个模块,用户可以在文本区域中输入文本,以及一些具有以下格式的图像标签:

ii[5ae71206|100|100]ii . ii[5ae71206|100|100]ii

This is how I 'm showing the text entered: 这是我显示输入文本的方式:

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

The "imageFilter"-filter is supposed to replace my custom tag from the text with an <img> , so ii[5ae71206|100|100]ii becomes: “imageFilter”-filter应该用<img>替换我的自定义标签,因此ii[5ae71206|100|100]ii变为:

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

The source code for my filter is as follows: 我的过滤器的源代码如下:

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;
                }
            };
        });
    }
);

The filter DOES return the correct string, but the view isn't rendering the image. 过滤器DOES返回正确的字符串,但视图不呈现图像。 When I'm just entering some text without my custom image-tag, everything is working as intended. 当我刚刚输入一些没有自定义图像标签的文本时,一切都按预期工作。 Any ideas? 有任何想法吗?

Just use plain src attribute since ng-src will need to $compile the HTML again. 只需使用普通的src属性,因为ng-src需要再次$编译HTML。 Here is a similar working example. 这是一个类似的工作示例。

 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> 

I wrote simple demo with filter that works for me: 我用过滤器编写了简单的演示版,对我有用:

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

JS 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 演示Plunker

So problem inside filter logic 过滤逻辑里面的问题

Alright, I think I located the problem: I do have a REST-service call inside my filter, which I removed for my initial post, since I thought it wasn't important and would only confuse people: 好吧,我想我找到了问题:我在我的过滤器中有一个REST服务调用,我删除了我的初始帖子,因为我认为它不重要,只会让人混淆:

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;
                }
            };
        });
    }
);

I solved this by adding another array to my controller, which maps the image tag to the image-id, which I need to get the proper image from the REST-service. 我通过向控制器添加另一个数组来解决这个问题,该控制器将图像标记映射到image-id,我需要从REST服务中获取正确的图像。 This array will be passed to the filter: 此数组将传递给过滤器:

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

The filter then looks like this: 然后过滤器看起来像这样:

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;
                }
            };
        });
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM