【问题标题】:Angular js scope variable (array) changes not reflected in ng-repeatAngular js范围变量(数组)更改未反映在ng-repeat中
【发布时间】:2017-04-06 18:11:13
【问题描述】:

我正在绑定一个范围变量,一个数组到 ng-repeat div 标签(基本上是一个表)。 当我动态地将任何数据添加到数组中时,它可以工作!一行被添加到表中。

但是当我从数组中删除一个元素时,更改不会反映在表格上。应该删除一行。

以下是我正在使用的代码(Javascript):

    $scope.myfields = [];
    $scope.addField = function () {
        $scope.myfields.push({ "name": "", "type": "", "required": "", "enum": "" });
        console.log("add: " + $scope.myfields.length);
        console.log(JSON.stringify($scope.myfields));
    }

    $scope.removeField = function (index) {
        $scope.myfields.splice(index, 1);
        console.log("remove: " + $scope.myfields.length);
        console.log(JSON.stringify($scope.myfields));
    }

EJS:请看下文!

奇怪的是, 在控制台日志中,它表示已按预期对 $scope 变量进行了更改,只有 view(table) 没有更新。 如果我不输入“track by $index”,添加和删除两个停止反映在表中!

任何帮助表示赞赏。谢谢!

编辑 2: 您要求的代码:

<div class="col-md-12">
                    <p style="text-align:center"><strong>DEFINE CUSTOM FIELDS:</strong></p>
                    <br>
                    <div style="text-align:center">
                        Click on '+' button to add custom field:
                        <div class="fa fa-plus-circle" ng-click='addField()'> </div>
                        <div class="fa fa-minus-circle" ng-click='removeField(0)'> </div>
                    </div>
                    <br>
                    <div  data-responsive-table>
                        <table  data-table>
                            <thead >
                            <tr >
                                <th  data-event='sort'>
                                Field Name
                                </th>
                                <th  data-event='sort'>
                                Type
                                </th>
                                <th data-event='sort'>
                                Is Required?
                                </th>
                                <th  data-event='sort'>
                                Enumeration
                                </th>
                            </tr>
                            </thead>
                            <tbody >
                            <tr  data-parent-row ng-repeat="um in dynamicFields track by $index">
                                <td>
                                    <input placeholder="Name" ng-model="um.name" validation="required" >
                                </td>
                                <td>
                                    <select style='height: 45px;'   ng-model="um.type" >
                                        <option value="string">string</option>
                                        <option value="boolean">boolean</option>
                                        <option value="integer">integer</option>
                                    </select>
                                </td>
                                <td>
                                    <select style='height: 45px;'   ng-model="um.required" >
                                        <option  value="true">true</option>
                                        <option value="false">false</option>
                                    </select>
                                </td>
                                <td>
                                    <input placeholder="Enum" ng-model="um.enum" validation="required" >
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>

【问题讨论】:

  • 发布调用 removeField() 和 addField() 以及定义 dynamicFields 数组的代码
  • 如何通过 ng-click 触发 removeField 函数?
  • 您的代码看起来不错。显示调用 removeField 的代码。
  • 请检查编辑!感谢您的投入..
  • 你总是删除 0 index ,我认为这可能是问题

标签: javascript angularjs arrays ejs


【解决方案1】:

你的 ng-repeat 中的变量名应该是 myfields 而不是 dynamicfields

因为在你的控制器中它是$scope.myfields,在你看来它应该是

ng-repeat="um in myfields track by $index"

【讨论】:

  • 问题编辑有误,代码中名称相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
相关资源
最近更新 更多