【问题标题】:ui-sref not working when generate dynamic content for datatable columns为数据表列生成动态内容时,ui-sref 不起作用
【发布时间】:2015-10-04 02:28:01
【问题描述】:

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

Table 1 :

ID | Name | Action 

1  | Jack | Edit

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>

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

请指教。

【问题讨论】:

    标签: angularjs angular-ui-router ui-sref


    【解决方案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);
    })
    

    【讨论】:

      【解决方案2】:

      我的方式:

      数据表设置选项:

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

      【讨论】:

      • 非常感谢阿巴斯,我花了 2 天时间才找到这个解决方案,它就像魅力一样。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多