【问题标题】:Using jQuery with Directives in AngularJS在 AngularJS 中使用带有指令的 jQuery
【发布时间】:2015-03-11 18:34:47
【问题描述】:

我正在使用 Angular UI 引导选项卡和 jQuery 数据表。然而,用于实例化数据表的 jQuery 在 AngularJS 处理指令之前被触发。请指教。

<tabset>
    <tab heading="Heading 1">
        <div id="my_div_container">Some dynamic content</div>
        <table id="myTable">
          <tr>
             <th>Column 1</th>
             <th>Column 2</th>
             <th>Column 3</th>
          </tr>
          <tr>
             <td>Value 1</td>
             <td>Value 2</td>
             <td>Value 3</td>
          </tr>
    </tab>
</tabset>

<script type="text/javascript">
    jQuery("document").ready(
        function($) {
            $('#my_div_container').html('my jQuery content');
            $("#myTable").dataTable({
                 aLengthMenu : [ [ 10, 25, 50, 100, -1 ],
                    [ 10, 25, 50, 100, "All" ] ]
            });
        }
    );
</script>

【问题讨论】:

  • 将 jquery 与 angular 一起使用.. 是代码异味.. 编写一个 angular 指令来处理这种情况
  • Tabset 是一个指令:angular-ui.github.io/bootstrap/#/tabs。我不认为 jQuery 数据表有一个指令(无论如何都很好),它做了很多。

标签: jquery angularjs angularjs-directive datatables jquery-datatables


【解决方案1】:

试试这个解决方案。

<tabset has-datagrid>
   <tab></tab>
</tabset>

module.directive('hasDatagrid', function () {
    return {
        link: function (scope, element) {
            // angular finished the processing the tabset and tab now. 
            // ok to access the dom at this point
            element.find('#my_div_container').html('my jQuery content');
            element.find("#myTable").dataTable({
                    aLengthMenu : [ [ 10, 25, 50, 100, -1 ],[ 10, 25, 50, 100, "All" ] ]
            });
        }
    }
});

【讨论】:

  • 它是 Angular UI 中的指令:angular-ui.github.io/bootstrap/#/tabs。我有一个缩小版。我需要获取未缩小和编辑链接功能吗?我不希望每次使用该指令时都会发生这种情况。理想情况下,我想要一个 tabset.ready() 事件。
  • 这是个好主意...在添加选项卡内容之前触发链接功能:
  • 得到它的工作......只需将 has-datagrid 放在选项卡内的表格/元素本身上。谢谢!
猜你喜欢
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多