【问题标题】:Inserting table header with a directive使用指令插入表头
【发布时间】:2015-10-24 10:08:45
【问题描述】:

我正在尝试使用角度指令插入表头。但是,它不是进入所需的thead 内部,而是放置在table 元素之外。 这是代码笔:http://codepen.io/sachingoel0101/pen/yYvmGV

<table class="table table-bordered table-striped">
    <thead>
        <sortingheader/>
    </thead>
    <tbody>
    </tbody>
</table>

只会变成:

<tr>
    <th>Sushi Roll</th>
    <th>Fish type</th>
    <th>Taste Level</th>
</tr>
<table class="table table-bordered table-striped">
    <thead></thead>
    <tbody></tbody>
</table>

这是指令代码:

.directive('sortingheader', function() {
    return {
        replace: true,
        template: "<tr><th>Sushi Roll</th><th>Fish type</th><th>Taste Level</th></tr>"
    }
})

我做错了什么?

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    您需要将指令restrict 属性更改为:

    restrict:'A'
    

    在 HTML 中:

    <tr sortingheader></tr>
    

    Working solution

    【讨论】:

    • 独立元素在这里不起作用的任何特殊原因?我没用IE,貌似有些问题。
    • 我不知道为什么,但我遇到了同样的问题。
    • 原因是 是无效的 HTML,所以浏览器在完全涉及 Angular 之前将其移出表格。请参阅此处的 文档,“允许的内容:零个或多个 元素”。 developer.mozilla.org/en/docs/Web/HTML/Element/thead
    猜你喜欢
    • 2014-10-05
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多