我怎么能成transclude属性? [英] How can I transclude into an attribute?

查看:165
本文介绍了我怎么能成transclude属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能以某种方式使用 ngTransclude 的属性值,而不是替换内部HTML内容?比如这个简单的指令

Is is possible to somehow use ngTransclude for an attribute value, instead of replacing inner HTML content? For example this simple directive

var testapp = angular.module('testapp', [])

testapp.directive('tag', function() {
  return {
    template: '<h1><a href="{{transcludeHere}}" ng-transclude></a></h1>',
    restrict: 'E',
    transclude: true
  }
});

和使用它作为

<tag>foo</tag>

我希望它翻译成

<h1><a href="foo">foo</a></h1>

是否有这样做的任何方式,或做我必须使用一个属性,而不是transcluding的?

Is there any way of doing that, or do I have to use an attribute instead of transcluding?

这里有一个小提琴的例子

Here's a fiddle with the example

推荐答案

事情是这样的:

var testapp = angular.module('testapp', [])

testapp.directive('tag', function() {
  return {
    restrict: 'E',
    template: '<h1><a href="{{transcluded_content}}">{{transcluded_content}}</a></h1>',
    replace: true,
    transclude: true,
    compile: function compile(tElement, tAttrs, transclude) {
        return {
            pre: function(scope) {
                transclude(scope, function(clone) {
                  scope.transcluded_content = clone[0].textContent;
                });
            }
        }
    }
  }
});​

小提琴

这篇关于我怎么能成transclude属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆