简体   繁体   English

angularjs-ng-repeat不更新数组更新

[英]angularjs - ng-repeat not updating on update of array

My AngularJS app displays data in a table using ng-repeat . 我的AngularJS应用使用ng-repeat在表中显示数据。 AngularJS makes a GET request, which retrieves an array. AngularJS发出一个GET请求,该请求检索一个数组。 The table correctly displays the elements of the array. 该表正确显示了数组的元素。

The page contains a button that calls a function to modify the array. 该页面包含一个按钮,该按钮调用一个函数来修改阵列。 However, the table is not updated to reflect the modified array. 但是,该表不会更新以反映修改后的数组。

I have tried the following: 我尝试了以下方法:

  • Calling $scope.apply() after the update. 更新后调用$scope.apply()
  • Enclosing the update within a function passed to $scope.apply() as in $scope.apply(function...) . $scope.apply(function...)一样,将更新包含在传递给$scope.apply() $scope.apply(function...) Link 链接
  • Using $timeout Link 使用$timeout 链接

None of these attempts worked. 这些尝试均无济于事。 Additionally, I believe that ng-repeat calls apply() itself. 另外,我相信ng-repeat调用apply()本身。

AngularJS AngularJS

<script>
    var app2 = angular.module('myApp2', []);

    app2.controller('StocksController', function ($scope, $http, $timeout) {
        // When the page loads...
        // Load myData with REST GET
        $http.get("http://example.com/exWebAPI/api/Ticker/").then(function (response) {
            $scope.myData = response.data;
        });

        // Update the array
        $scope.SendData = function () {
            alert('Hello from SendData.');
            $scope.myData.splice(0, 2);
            $scope.$apply();
        };

    });
</script>

HTML HTML

<div ng-app="myApp2" ng-controller="StocksController">
    <h2>Reportable Tickers</h2>

    <p>
        <a href="#" ng-click='AddTicker()'>Add Ticker</a>
    </p>

    <table class="table">
        <tr>
            <th>
                Symbol
            </th>
            <th></th>
        </tr>

        <tr ng-repeat="x in myData">
            <td>
                {{ x.Ticker }}
            </td>
            <td>
                <a href="#" ng-click='EditTicker(x.Ticker)'>Edit</a> |
                <a href="#" ng-click='DetailsTicker(x.Ticker)'>Details</a> |
                <a href="#" ng-click='DeleteTicker(x.Ticker)'>Delete</a>
            </td>
        </tr>

    </table>

    <hr />
    <div>

        <div ng-controller="StocksController">
            <button ng-click="SendData()">Submit</button>
            <button ng-click="QueryData()">Query Data</button>
            <hr />
        </div>
    </div>
</div>

Your issue is that you have the controller defined twice. 您的问题是您定义了两次控制器。

If you're calling the controller twice it will call a new instance of the same controller. 如果您两次调用控制器,它将调用同一控制器的新实例。

Meaning that the $scope you're manipulating on the button isn't the same as the $scope on the ng-repeat. 这意味着您在按钮上操作的$ scope与ng-repeat上的$ scope不同。

Demo of your issue: https://jsfiddle.net/U3pVM/26753/ (showing different scopes) 您的问题的演示: https : //jsfiddle.net/U3pVM/26753/ (显示不同的范围)

Remove the ng-controller attribute around the parent of the button, and wrap your whole template. 删除按钮父控件周围的ng-controller属性,然后包装整个模板。

<div ng-app="myApp2" ng-controller="StocksController">
  <table class="table">
    <tr>
      <th>
        Symbol
      </th>
      <th></th>
    </tr>

    <tr ng-repeat="x in myData">
      <td>
        {{ x.Ticker }}
      </td>
      <td>
        <a href="#" ng-click='EditTicker(x.Ticker)'>Edit</a> |
        <a href="#" ng-click='DetailsTicker(x.Ticker)'>Details</a> |
        <a href="#" ng-click='DeleteTicker(x.Ticker)'>Delete</a>
      </td>
    </tr>

  </table>

  <hr />
  <div>

    <div>
      <button ng-click="SendData()">Submit</button>
      <hr />
    </div>
  </div>
</div>

Working example: https://jsfiddle.net/U3pVM/26752/ 工作示例: https : //jsfiddle.net/U3pVM/26752/

Note: I've removed the $http request inside just for demo purposes. 注意:我删除了$http请求,只是出于演示目的。

Additionally, seeing as you're using ng-click to with SendData there's no need to run scope.$apply(); 另外,看到您正在使用ng-click来使用SendData ,无需运行scope.$apply(); SendData as ng-click will trigger a new digest cycle internally. 因为ng-click会在内部触发一个新的摘要周期。

You just have defined your myData variable in myApp2 module and its controller scope, However the displaying array table is out of your module scope ng-app . 您刚刚在myApp2模块及其控制器范围内定义了myData变量,但是显示数组表不在模块范围ng-app范围ng-app Surround your table html code with ng-app , It may work correctly. ng-app包围表格html代码,它可能会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM