Search code examples
angularjsangular-ui-routerui-sref

ui-sref not working when generate dynamic content for datatable columns


I'd to link a column of my data-table to a dynamic Angularjs view.

Table 1 :

ID | Name | Action 

1  | Jack | Edit

Edit should be a link to #/clients/1/edit

/clients/:id/edit (app.client_edit) is already created and it's working.

I'm trying the code below:

$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'
};

Given result:

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

Expected result:

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

When I put < a ui-sref="app.client_view({'id': '1'})">test< / a> on the page statically it's working but not sure why it doesn't work when it's dynamically generated.

Please advise.


Solution

  • my way:

    DataTable set options:

    "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>';
           }
        }
    ]