2014-01-07 120 views
4

考虑下面的代码(http://jsbin.com/IfejIWES/1/):Transclusion(真) - 结合值

HTML:

<!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js"></script> 
<meta charset=utf-8 /> 
<title>JS Bin</title> 
</head> 
<body> 
    <div ng-controller="MyCtrl"> 

    <div my-directive> 
     <button>some button</button> 
     <a href="#">and a link</a> 
    </div> 
</div> 
</body> 
</html> 

JS:

var myApp = angular.module('myApp',[]); 

function MyCtrl($scope) { 
    //$scope.log=[]; 
} 

myApp.directive('myDirective', function(){ 
    return{ 
     transclude: true, 
     template: '<div class="something" ng-transclude> This is my directive content</div>' 
    }; 
}); 

使用版本AngularJS的1.1.3,输出合并了从my-directive(在HTML中)的按钮和锚点与模板板内文本'这是我的指令内容'。

如果我将版本更改为1.2.1 my-directive content 将替换模板的内部文本。

有没有办法让角度1.2.1(和更高版本)做更旧的行为?

回答

1

不,这是一个非常有意的改变。见this commit

0

Jeff Hubbard提供的链接(感谢Jeff)让我朝着正确的方向发展。从该帖子的评论有人(https://github.com/angular/angular.js/commit/eed299a31b5a6dd0363133c5f9271bf33d090c94#commitcomment-4685184)发布了一项工作:http://plnkr.co/edit/EjO8SpUT91PuXP0RMuJx?p=preview

本质上,您可以通过更改JavaScript以在单独的指令中使用transcludeFn函数来获取旧的行为。请参阅下面我更新的代码:

var myApp = angular.module('myApp',[]); 

function MyCtrl($scope) { 

} 

myApp.directive('myDirective', function(){ 
    return{ 
     transclude: true, 
     template: '<div class="something" ng-transclude-append> This is my directive content</div>'  
    }; 
}); 

myApp.directive('ngTranscludeAppend', function() { 
    return function(scope, element, attrs, ctrl, transcludeFn) { 
     if (!transcludeFn) return; 

     transcludeFn(function(clone) { 
     element.append(clone); 
     }); 
    }; 
}); 

链接到我的更新jsbin:http://jsbin.com/IfejIWES/3/

最后一点,我想在我的链接功能直接嵌入transcludeFn这样的:

link: function(scope, element, attrs, ctrl, transcludeFn) {   
    transcludeFn(function(clone) { 
    element.append(clone); 
    }); 
} 

但这有创建按钮并锚定两次的效果。将它转化为自己的指令为我解决了它。