5

这里是我的代码Transclude在链接功能不注射

'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。 我需要将父范围注入transcluded作用域。 transclude变量是未定义的。我哪里错了。

我的角度版本是1.1.5

谢谢。

+0

我可以看看'item.html'? –

回答

0

你在找什么是transcludeFn。试试这个:

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

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

var controller = scope.controller; 
+0

我明白transclude是由angular定义的,在我的情况下,它的undefined属于我的链接函数。那是什么问题。 –

+0

我的问题是,你为什么要访问它的链接功能?它是指令定义的一部分,它将完成它的工作。你是否试图有条件地打开/关闭跨界? –

+0

我想这[这个](http://stackoverflow.com/questions/25641618/angular-transclude-ng-repeat-inside-directive#answer-27144559) –