[英]AngularJS get directive HTML before angular replace it and use it in the code
I would like to make an universal directive in which the user can easily insert any HTML for the header and footer. 我想做出一个通用指令,用户可以在其中轻松地为页眉和页脚插入任何HTML。
This is my HTML code: 这是我的HTML代码:
<datagrid
id="testing"
url="google.com/test">
<header>
<p>header</p>
</header>
<footer>
<p>footer</p>
</footer>
</datagrid>
And I would like to get the inner HTML and parse it on my own. 我想获取内部HTML并自行解析。
directive('datagrid', function () {
return {
restrict: 'E',
templateUrl: 'datagrid.htm',
scope: true,
transclude: true,
link: function ($scope, $el, $attr) {
// get inner HTML, separate header and footer HTML to the variables
// and add that HTML later in the template
}
};
});
My template: 我的模板:
<script type='text/ng-template' id='datagrid.htm'>
<table class="datagrid table table-hover table-condensed table-striped">
<thead>
<tr class="top-actions-container">
<!-- INJECT HEADER HTML -->
<th></th>
</tr>
</thead>
<tbody>
<tr>
<!-- RESULTS -->
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
<!-- INJECT FOOTER HTML -->
<td></td>
</tr>
</tfoot>
</table>
</script>
I'm new to the Angular so I'm open to other suggestions. 我是Angular的新手,所以我愿意接受其他建议。 If that's possible is this a good way to do it? 如果可能的话,这是个好方法吗?
You can try this: 您可以尝试以下方法:
Usages (in main html) 用法(在主要html中)
<datagrid
id="testing"
url="google.com/test">
<!--In HEADER I used <table> tag because for some reasons (?) <tr>, <td> tags do not work without <table> tag.-->
<header>
<table><tr><td>One</td><td>Two</td><td>Three</td></tr></table>
</header>
<footer>
<p>footer</p>
</footer>
</datagrid>
Directive JS 指令JS
app.directive('datagrid', function(){
return {
restrict: 'EA',
scope: true,
transclude: true,
templateUrl: '../../datagrid.html',
link: function(scope, element, attr){
var header = element.find('header').html();
element.find('header').remove();
element.find('thead').html(header);
}
};
});
You can write same code for footer. 您可以为页脚编写相同的代码。
datagrid.html datagrid.html
<table class="datagrid table table-hover table-condensed table-striped">
<!-- Keep thead tag empty -->
<thead></thead>
<tbody>
<tr>
<!-- RESULTS -->
<td></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
<div ng-transclude></div>
<!-- ng-transclude will include all custom html defined under <datagrid> directive -->
I hope this will help you. 我希望这能帮到您。
I just had to use an arg $transclude in the controller of the directive. 我只需要在指令的控制器中使用arg $ transclude。
// declaration of DataTable directive
.directive('datagrid', function () {
return {
restrict: 'E',
templateUrl: 'datagrid.htm',
scope: true,
transclude: true,
link: function ($scope, $el, $attr) {
var dtData = {
id: $attr.id,
url: $attr.url
};
if ($scope['datagrid'] === undefined) $scope['datagrid'] = [];
$scope['datagrid'].push([$attr.id, dtData]);
},
controller: function ($scope, $transclude) {
$transclude(function (clone, scope) {
console.log($(clone).find('header')); // don't work
$.each(clone, function (i, e) {
if ($(e).is('header')) console.log(e); //works
});
});
}
};
});
From this I believe that I can accomplish my goal. 由此,我相信我可以实现自己的目标。 Only questions that are still there are if this is a good way to do this, will that confuse other users of my plugin and why the hell can't I select the "header" element with the jQuery find method? 如果这是一个好方法,只有问题仍然存在,这会使我的插件的其他用户感到困惑,为什么我不能用jQuery find方法选择“ header”元素? :) :)
Thank you all for the help. 谢谢大家的帮助。 Hope I will soon finish the rest of the goal so I can post it here. 希望我能尽快完成剩余的目标,以便可以在此处发布。 There shouldn't be any more problems to accomplish it. 完成该任务应该不再有任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.