javascript - 无法使用 Angular.js 从表中删除预期的行

标签 javascript angularjs

我有一个问题。我需要使用 Angular.js 从我的应用程序的表结构中删除一个特定行。我的代码位于下面的 plunkr 链接中。

code is present here

在上面的代码中,为星期一创建 3 行,并填充所需的数据,每一行都应该不同。使用 - 按钮删除中间行,您可以检查最后一行子类别显示错误。这是我的问题。如果我要删除任何特定行,则该行数据应从数组中删除,而其他行将与所选数据保持不变。请帮助我。

最佳答案

我编辑了你的骗子:https://plnkr.co/edit/4okaCC?p=preview

您的问题在于您在 ng-repaet 中使用 $index 。当您删除中间行时,第三行的 $index 会发生变化,并且您引用了错误的类别。 我将您的 plunker 更改为使用根据所选类别返回子类别的函数:

$scope.getSubCategoryFor = function(answer) {
if(!answer.category) {
  return [];
}

  console.log(answer);
  var result = $filter('filter')(subcategories, {id:answer.category.id});
  console.log('result', result);
  return result;
}

我在 html 中使用它:

<table>
        <tbody>
          <tr ng-repeat="answer in d.answers">
            <td>
              <select class="form-control" id="answer_{{$index}}_subcategory" name="answer_{{$index}}_subcategory" ng-model="answer.subcategory" ng-options="sub.name for sub in getSubCategoryFor(answer)">
                <option value="">Select Subcategory</option>
              </select>
            </td>
          </tr>
        </tbody>
      </table>

希望这对你有帮助

关于javascript - 无法使用 Angular.js 从表中删除预期的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428566/

相关文章:

javascript - 我不明白这个错误 : dataobj is not a function

javascript - 在 React Native 中,我的代码没有呈现任何内容

javascript - 来自一张纸的事件将时间戳转储到另一张纸上

javascript - Coffeescript 中某种 _ref 的用法?

javascript - 了解 AngularJS $compile 函数

javascript - 如何在日期字段中设置初始值或默认值?

javascript - ng-repeat 使用 Angular 中的 json 数据与 optgroup 进行选择

javascript - 如何将 Backbone.js 与 Symfony 框架和/或 Apache Thrift 一起使用

jquery - 动态表单字段上的 angularjs datepicker 指令 "Missing instance data"

node.js - 测试 AngularJS Express Node 应用程序