【问题标题】:Transclude not injected in link functionTransclude 未注入链接功能
【发布时间】:2015-09-18 06:45:02
【问题描述】:

这是我的代码

'use strict';
angular.module('app')
    .directive('item'
            , ["$timeout"
            , "$Service"
            , function(
                $timeout
                , $utils) {
    return {
        restrict: 'A',
        scope: {
            item: '=',
        },
        transclude: true,
        link: function(scope, element, attrs, ctrl, transclude){
        },
        templateUrl: $fsUtils.getRelativeUrl('templates/item.html'),
        controller: 'ItemCtrl', 
    };
}]);

我的 index.html:

<item><div>Transcluded content.</div></item>

transclude 变量为undefined,ctrl 变量为proto__: Object。 我需要将父范围注入到嵌入范围中。 transclude 变量未定义。我哪里错了。

我的角度版本是 1.1.5

谢谢。

【问题讨论】:

  • 我可以看到item.html吗?

标签: javascript angularjs angularjs-directive angularjs-ng-transclude


【解决方案1】:

您正在寻找的是 transcludeFn。试试这个:

transclude: true,
transcludeFn: function () { /*do your stuff here*/ },
...
link: function(scope, element, attrs, controller, transcludeFn)

要在链接功能中访问控制器,您可以这样做:

var controller = scope.controller;

【讨论】:

  • 我知道 transclude 是由 angular 预定义的,在我的情况下,它在我的链接函数中未定义。这就是问题所在。
  • 我的问题是为什么要在链接功能中访问它?它是指令定义的一部分,它会完成它的工作。您是否尝试有条件地打开/关闭嵌入?
  • 我想要这个this
  • 您找到解决方案了吗?由于某种原因,我目前遇到了同样的问题...这通常可以正常工作,但我的代码必须搞砸了,但看不到什么!
猜你喜欢
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多