AngularJS在angular替换它并在代码中使用它之前获取HTML指令
AngularJS get directive HTML before angular replace it and use it in the code
我想做一个通用指令,用户可以很容易地为页眉和页脚插入任何HTML。
这是我的HTML代码:<datagrid
id="testing"
url="google.com/test">
<header>
<p>header</p>
</header>
<footer>
<p>footer</p>
</footer>
</datagrid>
我想得到内部的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
}
};
});
我的模板:<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>
我是Angular的新手,所以我愿意接受其他建议。如果有可能,这是一个好方法吗?
你可以试试:
用法(在主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>
指令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);
}
};
});
您可以为footer编写相同的代码
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 -->
我只需要在指令的控制器中使用一个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
});
});
}
};
});
由此,我相信我能实现我的目标。唯一的问题仍然存在,如果这是一个很好的方式来做到这一点,会混淆我的插件的其他用户,为什么我不能选择"header"元素与jQuery查找方法?:) 谢谢大家的帮助。希望我能尽快完成剩下的目标,这样我就可以把它贴在这里了。完成它应该不会再有任何问题了。
相关文章:
- 获取HTML属性中CSS声明的值
- 使用Java从javascript链接获取html更改
- 从模板标签获取html
- 无法获取@Html.ListBoxFor()的正确语法
- 获取html表单信息并使用ajax将其推送到PHP
- 获取HTML的第一句话
- PHP和JavaScript:在PHP中获取HTML值
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 如何获取html中输入框的值并对其进行操作
- Javascript从外部站点获取html
- 在不使用sort()的情况下获取HTML LI元素的副本
- 如何在单击行时获取html表中第一列的值
- 在Javascript服务器应用程序之后获取HTML
- 如何使用位置获取滚动功能获取 HTML 中的元素标签
- 如何使用 JQuery 从相对元素获取 html 文本
- 从 Rails dom_id 方法获取 HTML 类名
- TypeScript - 如何获取 HTML 元素,例如在 Javascript 中使用美元符号 ($)
- 为什么我的“for”循环只获取 html 中的最后一项
- objective-C 在 UIWebView 中的 ajax 调用后获取 HTML 值
- 从用户那里获取 HTML 输入