问题描述
我想包含< tr>
和< td>
,显然我不能这么做与指令。它会一直忽略< td>
或< td>
,就好像它们不存在一样。这是我想要完成的:
< my-table>
< tr>
< td> hello< / td>
< td>世界< / td>
< / tr>
< / my-table>
以下是javascript:
angular.module('app',[])
.controller('Controller',['$ scope',function($ scope){
} ])
.directive('myTable',function(){
return {
restrict:'E',
transclude:true,
templateUrl:'my- table.html'
};
}); b
$ b $ my $ table $ $ p> < table ng-transclude>
< / table>
上面的代码导致:
< my-table class =ng-isolate-scope>< table ng-transclude =>
hello< - no< tr>也没有< td>这里只是纯文本
world
< / table>< / my-table>
例如:
这不是一个交叉问题。这是无效html的问题,因为没有表的< tr>
无效。所以角度来自浏览器 text
,而不是DOM元素。因此,您需要在html中包含< table>
标签:
< my-table>
< table>
< tr>
< td> hello< / td>
< td>世界< / td>
< / tr>
< / table>
< / my-table>
然后您就可以访问 或使用 I want to include Here's the javascript: my-table.html : the code above resulted in: example : PLUNKR It's not a transclude problem. It's a problem with invalid html, because Then you'll be able to get access to or use 这篇关于如何包含< tr>角度指令中的元素(transclude)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! tbody $ c
$ b $ > link:function(scope,element,attrs,ctrls,transclude){
var html = transclude();
element.find('table')。append(html [1] .firstElementChild);
ng-transclude $ c $像你一样在你的模板中。不过,我可能会推测您稍后想要重新使用transcluded部分,因此在链接函数中访问它对我来说更有意义。
<tr>
and <td>
and apparently I can't do that with directive. It keeps ignoring <td>
or <td>
as if they don't exists. here's what I trying to accomplish:<my-table>
<tr>
<td>hello</td>
<td>world</td>
</tr>
</my-table>
angular.module('app', [])
.controller('Controller', ['$scope', function($scope) {
}])
.directive('myTable', function() {
return {
restrict: 'E',
transclude: true,
templateUrl: 'my-table.html'
};
});
<table ng-transclude>
</table>
<my-table class="ng-isolate-scope"><table ng-transclude="">
hello <-- no <tr> nor <td> here just plain text
world
</table></my-table>
<tr>
without a table is invalid. So angular gets from a browser text
, not DOM elements. So you need to have <table>
tag inside an html: <my-table>
<table>
<tr>
<td>hello</td>
<td>world</td>
</tr>
</table>
</my-table>
tbody
element created by a browser along with tr
's in link function and process it: link: function(scope,element,attrs,ctrls,transclude) {
var html = transclude();
element.find('table').append(html[1].firstElementChild);
}
ng-transclude
in your template as you did. However, I may presume that you'll want to reuse the transcluded part later, so accessing it in link function makes more sense to me.