【问题标题】:AngularJS/bootstrap -table: passing object with ng-click on data-formatter buttonAngularJS/bootstrap -table:通过 ng-click 数据格式化按钮传递对象
【发布时间】:2018-01-29 16:31:33
【问题描述】:

在通过 ng-click 数据格式化程序按钮传递对象时遇到一些问题,数据格式化程序是引导表功能的一部分。

这是我在 html 上的内容

<table  class="container" 
        data-pagination="true"
        data-page-list="[5, 10, 20, 50, 100, 200]"
        data-search="true"
        data-toggle="table"
        data-show-refresh="true"
        data-show-columns="true"
        data-toolbar="#customToolbar"
        id="currentTable">
   <thead>
     <tr>
        <th data-field="bundle" data-sortable="true">Name</th>
        <th data-field="status" data-sortable="true" data-cell-style="cellStyle">Status</th>
        <th data-field="createdTime" data-sortable="true" data-formatter="dateFormatter">Date Created</th>
        <th data-formatter="editButton"></th>
     </tr>
   </thead>
 </table>

<script type="text/javascript">
  function dateFormatter(value){
    var date = new Date(value);
    var formattedDate = date.getFullYear() + '-' + (date.getMonth() + 1 )+ '-' + date.getDate();
    return formattedDate;
  }

 function editButton(value, row){
   var currentRow = row;
   console.log(currentRow);
   var append = 'angular.element(this).scope().editName('+currentRow+')';
   return '<button class="editButton" onclick="'+append+'"><i class="fa fa-pencil fa-2x" aria-hidden="true"></i></button>';

 }
</script>

controller-js - 非常简单

$scope.editName = function(row){
        console.log(row);
    }

所以用户将点击编辑按钮,一旦点击它应该通过 ng-click 传递行对象,editBundle 函数应该在控制器中捕获,但我得到一个错误 -> Uncaught SyntaxError: Unexpected identifier

1 (function(event){angular.element(this).scope().editName([object Object])
2 })

但如果我在行前后添加双引号 editName("'+currentRow+'"),那么我会得到一个不同的错误 -> Uncaught SyntaxError: Unexpected token }

 1 (function(event){angular.element(this).scope().editName(
 2 })

在这里尝试不同的东西但没有结果,有人遇到过这种问题吗?我知道这是一些语法问题,但无法让它工作。

我使用引导表中的数据格式化程序的原因是,该表似乎从 api 调用中加载名称的速度比其他表格式快。我尝试使用 smart-table.js,但加载或尝试排序时速度很慢,数据不会显示。我对 smart-table.js 不是很熟悉。

【问题讨论】:

    标签: javascript html angularjs bootstrap-table


    【解决方案1】:

    我相信您收到此错误的原因是因为您正在注释掉引号 'angular.element(this).scope().editName('+currentRow+')' 并且您添加了 'angular.element(this).scope().editName("'+currentRow+'")' 使编辑名称中的所有值都在引号中并且不脱离括号尝试转义值或将双引号放在单引号内。看Escape quotes in JavaScript

    【讨论】:

      【解决方案2】:

      您可以使用下面的代码:

      'angular.element(this).scope().editName(' + JSON.stringify(row).replace(/"/g, '&quot;') + ')';
      

      你会在$scope.editName函数中得到对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多