简体   繁体   English

AngularJS:带有HTML和角度表达式的指令,使用外部范围“编译”内容

[英]AngularJS: directive with HTML and angular expressions, “compile” the contents with the outer scope

I'm new to AngularJS , sorry if I don't use the correct words for naming things. 我是AngularJS新手,很抱歉,如果我没有使用正确的词来命名事物。

I'm trying to implement a directive that can accept any kind of text (including HTML tags and angular expressions ). 我正在尝试实现一个directive ,该directive可以接受任何类型的文本(包括HTML标记和angular expressions )。 This directive needs to treat that text and do some stuff with it: for example, if it finds any image inside of it, the image is downloaded and the original URL is replaced with the local URL. directive需要处理该文本并对其进行处理:例如,如果在其中找到任何图像,则下载该图像并将原始URL替换为本地URL。

I need to " compile " the contents of the directive since some images might be in scope variables (ng-src="{{whatever}}"). 我需要“ compiledirective的内容,因为某些图像可能在scope变量中(ng-src =“ {{whatever}}”)。 I cannot know the names of the scope variables. 我不知道scope变量的名称。

I've read a lot of stuff about directives , transclude , shared/isolated scope , $compile , etc. but I can't make it work and the more I read the more confused I get. 我已经阅读了很多有关directivestransclude ,共享/隔离scope$compile等的内容,但是我无法使其正常工作,而且阅读的越多,我就越困惑。

I decided to start with a simple case: a directive that gets a simple angular expression (please notice that the 'whatever' name can change): 我决定从一个简单的案例开始:一个获得简单角度表达式的指令(请注意,“随便什么”名称都可以更改):

<format-text>{{whatever}}</format-text>

I want to get the text inside the 'whatever' variable inside of the directive. 我想在指令内部的“ whatever”变量中获取文本。 I tried a lot of things, but I can't get it to work. 我尝试了很多事情,但无法正常工作。 This is my latest test: 这是我最新的测试:

angular.module('mymodule').directive('formatText', function($compile) {

     return {
        restrict: 'E',
        scope: true,
        transclude: true,
        link: function(scope, element, attrs, ctrl, transclude) {
            transclude(scope, function(clone, outerScope) {
                var template = '<span>'+clone.html()+'</span>';
                var compiled = $compile(template)(outerScope);
                console.log(compiled.html());
            });
        }
    };
});

This directive writes the following in the console: 该指令在控制台中写入以下内容:

{{whatever}}

and I'd like to get the contents of the variable. 并且我想获取变量的内容。 The variable 'whatever' is defined in the scope and it's resolved ok in the view. 变量“ whatever”在范围内定义,并且在视图中可以正常解析。

Anyone knows how can I achieve what I'm trying to do? 谁知道我该怎么做?

EDIT: I created a jsfiddle for it, I don't know why the 'whatever' contents aren't written in the HTML but if you look at the console you'll see that inside compiled.html() the angular expression isn't replaced. 编辑:我为此创建了一个jsfiddle,我不知道为什么在HTML中没有写“任何”内容,但是如果您查看控制台,您会发现在compiled.html()里面的角度表达式是“取代。

http://jsfiddle.net/8ngcwxen/ http://jsfiddle.net/8ngcwxen/

Thank you! 谢谢!

You can get the content of that variable by using the $interpolate service: 您可以使用$interpolate服务获取该变量的内容:

var exp = $interpolate(clone.html());
var whatever = exp(scope);

In order to change the template of a directive before it is processed by Angular, you have to use the compile function. 为了在Angular处理指令之前更改指令的模板,必须使用compile函数。 A simple case is to get the contents as text, empty the contents, run your transformation in the text and set the transformed text back, eg: 一个简单的例子是获取内容为文本,清空内容,在文本中运行转换,然后将转换后的文本放回原位,例如:

app.directive('formatText', function() {
    return {
        restrict: 'E',
        scope: false,
        compile: function(tElem, tAttrs) {
            var html = tElem.html();

            tElem.empty();

            tElem.html(processTemplateText(html));

            return function() {
            };
        }
    };
});

As demonstrated in this fiddle: http://jsfiddle.net/ur4uzrz3/ 如该小提琴所示: http : //jsfiddle.net/ur4uzrz3/

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

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