2017-03-05 47 views
0

角度应用程序已被嵌入到一个jQuery应用程序,它使用数据表插件来显示表。 在此表中,按钮和链接的HTML是在加载页面并完成服务器端AJAX调用后生成的。如何在加载HTML后使用jQuery Datatables处理指令?

我写了一个属性级指令my-directive,它应用于那些生成的HTML按钮。 like <button my-directive>delete</button>

由于在初始化Angular APP之后生成HTML,因此my-directive属性无效。

如何在AJAX调用完成后处理指令?

(我是从DataTable的drawCallback方法捕捉AJAX调用完成的事件。

+0

而不是让我们猜测出蓝色的,请出示你的代码 – davidkonrad

+0

@davidkonrad:相关 - http://stackoverflow.com/questions/42648788/datatabes-how-to-get-pagination-working-当表-HTML-被编译与 - angul –

回答

0

的解决方案是

  1. 重新编译通过DataTable中采用了棱角分明生成的HTML。
  2. 用编译版本代替HTML

为此,我们应该在数据表配置中添加以下代码 -

var exampleTable = $("#table_id").DataTable({ 
"drawCallback": function(settings) { 

      angular.element(document).injector().invoke(['$compile', function ($compile) { 
        // Create a scope. 
        var $scope = angular.element(document.body).scope(); 
        // Specify what it is we'll be compiling. 
        var to_compile = $("#table_id").html(); 
        // Compile the tag, retrieving the compiled output. 
        var $compiled = $compile(to_compile)($scope); 
        // Ensure the scope and been signalled to digest our data. 
        $scope.$digest(); 
        // Replace the compiled output to the page. 
        $("#table_id").html($compiled); 
       }]) 

     }