微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 访问模板中的角度指令(元素)的文本

所以我关注了自定义组件的 this EggHead.io tutorial,我遇到这个问题.当声明一个指令如:
angular.module('myApp',[])
    .directive('myDir',function(){
        return {
            restrict: "E",controller: "myController",link: function(scope,element,attrs) {
                scope.foo = element.text();
            },templateUrl: "myDirTemplate.html"
        }
    });

而模板是:

<div>The value is: {{foo}}</div>

并使用如下指令:

<html>
...
<myDir>Bar</myDir> 
...
</html>

链接功能中的元素是指

<div>The value is: {{foo}}</div>

在模板中.如果我没有指定templateUrl,那么元素指的是

<myDir>Bar</myDir>

在主视图中,这是我想要的.我希望这个指令能够将“Bar”文本插入到{{foo}}中,给出最终结果:

<div>The value is: Bar</div>

但是我不知道如何在每个角度选择模板的元素.

有任何想法吗?

解决方法

您需要使用 ngTransclude
app.directive('myDir',function(){
  return {
    restrict: "E",transclude: true,templateUrl: "myDirTemplate.html",replace: true
  }
});

然后你的外部模板变成类似于:

<div>The value is: <span ng-transclude></span></div>

查看此处的代码http://plnkr.co/edit/EuT5UlgyxgM8d54pTpOr?p=preview

原文地址:https://www.jb51.cc/js/153322.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐