5

我想将我的数据表的一列链接到动态 Angularjs 视图。

Table 1 :

ID | Name | Action 

1  | Jack | Edit

编辑应该是指向 #/clients/1/edit 的链接

/clients/:id/edit (app.client_edit) 已经创建并且正在工作。

我正在尝试下面的代码:

$scope.dataTableOpt = {
 "columnDefs": [
    {
       "targets": 0,
       "render": function ( data ) {
          return "<a ui-sref=\"app.client_view({id: $row[0]})\">Edit</a>";
       }
    }
 ],
 'ajax': 'http://localhost/admin/clients'
};

给定结果:

Link1 = < a ui-sref="app.client_view({'id': '1'})">edit</ a>

预期结果:

Link2 = < a ui-sref="app.client_view({id: '1'})" class="ng-scope" href="#!/client/2">edit</ a>

当我< a ui-sref="app.client_view({'id': '1'})">test< / a>静态放置页面时它正在工作,但不确定为什么它在动态生成时不起作用。

请指教。

4

2 回答 2

9

我的方式:

DataTable 设置选项:

"columnDefs": [
   {
      "targets": [0, 1],
      "render": function ( data, type, row ) {
         var href = $compile('<a ui-sref="stateName({id: ' + $stateParams.id + '})"></a>')($scope)[0].href;
         return '<a href="' + href + '">' + data + '</a>';
       }
    }
]
于 2015-08-03T09:12:19.377 回答
1

@Abbas Moazzemi

如果使用.withOption('createdRow'如下,你不需要使用$compile

.withOption('createdRow', function(row) {
    // Recompiling so we can bind Angular directive to the DT
    $compile(angular.element(row).contents())($scope);
})
于 2017-12-23T05:44:21.680 回答