javascript - Angularjs 在当前行位置添加行

标签 javascript angularjs

我有以下代码,假设在当前行位置添加另一行(而不是在行的底部):

<tr ng-repeat="ro in rows">
  <td>{{ro.name}}</td>
  <td>{{ro.id}}</td>
  <td><a ng-click="addRo($index)">add row</a></td>
</tr>

在我的 Controller 中,我有:

$scope.addRo=function(index){
     $scope.inderted={
       id: '',
       name: ''
      };
      $scope.rows($index+1).push($scope.inserted);
}

我尝试了上面的代码,希望将索引添加到当前位置,它会添加它,但它不起作用。如何解决这个问题?

最佳答案

尝试使用.splice()而不是.push()

$scope.addRo=function(index){
  var inderted={
    id: '',
    name: ''
  };
  $scope.rows.splice(index + 1, 0, inderted);
}

这是docs对于 .splice()

此外,如果您只是暂时使用它,则无需将“inderted”添加到范围中。

关于javascript - Angularjs 在当前行位置添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421362/

相关文章:

Javascript相当于对象模型中的析构函数

javascript - 启动时虚拟地球 map 不显示为显示 :none

javascript - 切换场景时黑屏 Phaser 3

javascript - Yii Cgridview $.fn.yiiGridView.getSelection 函数不能立即工作

javascript - 复制 $rootScope

javascript - 使用 D3.js、TypeScript 和 Angular 绘制饼图时出现编译错误

javascript - 在页面 anchor 滚动和偏移标题

javascript - Angularjs 使用 momentjs 获取总时间

angularjs - karma – 带有 templateUrl 的 Angular 单元测试指令失败,即使使用带有 “unexpected request” 的 ng-html2js

rest - Angular JS : Full example of GET/POST/DELETE/PUT client for a REST/CRUD backend?