Angular 范围变量将 $http 的结果附加到数据变量而不是覆盖它
Angular Scope variable appends result from $http to data variable instead of overwriting it
我有以下 Angular 函数,它通过在 $interval
上调用的 $http
调用自动刷新视图中的数据
app.controller("processModel", function ($scope, $http, $interval) {
$scope.data
$scope.LoadData = function() {
$http({
method: "GET",
url: 'Home/DataRefresh',
params:{'_': +new Date() }
}).then(function success(response) {
$scope.data = response.data;
}, function error(errResponse) {
alert("data " + errResponse.data + " status " + errResponse.status + " headers " + errResponse.headers + "config " + errResponse.config + " statusText " + errResponse + " xhrStat " + errResponse.xhrStatus);
});
};
// Initial data load
$scope.LoadData();
// Refresh data on an interval
$interval($scope.LoadData, 10000);
});
被调用,数据显示如下HTML
<main class="container" role="main" ng-controller="processModel">
<div class="jumbotron">
<table>
<tbody>
<tr ng-repeat="item in data">
<td>{{item.info}}</td>
</tr>
</tbody>
</table>
</div>
</main>
我遇到的问题是,当每 10 秒调用一次 Home/DataRefresh 时,视图只是将新的(和重复的)项目添加到现有 table 的末尾,而不是覆盖它。所以它要么将新值附加到 $scope.data
,要么不会自动重做 ng-repeat
.
构建的 table
我该怎么做才能让它用从 Home/DataRefresh 获得的新数据覆盖 table 的内容?
这是服务器端代码的问题,它在对数据库进行新的 Get 调用时没有清除缓存。 angular 工作正常。
我有以下 Angular 函数,它通过在 $interval
$http
调用自动刷新视图中的数据
app.controller("processModel", function ($scope, $http, $interval) {
$scope.data
$scope.LoadData = function() {
$http({
method: "GET",
url: 'Home/DataRefresh',
params:{'_': +new Date() }
}).then(function success(response) {
$scope.data = response.data;
}, function error(errResponse) {
alert("data " + errResponse.data + " status " + errResponse.status + " headers " + errResponse.headers + "config " + errResponse.config + " statusText " + errResponse + " xhrStat " + errResponse.xhrStatus);
});
};
// Initial data load
$scope.LoadData();
// Refresh data on an interval
$interval($scope.LoadData, 10000);
});
被调用,数据显示如下HTML
<main class="container" role="main" ng-controller="processModel">
<div class="jumbotron">
<table>
<tbody>
<tr ng-repeat="item in data">
<td>{{item.info}}</td>
</tr>
</tbody>
</table>
</div>
</main>
我遇到的问题是,当每 10 秒调用一次 Home/DataRefresh 时,视图只是将新的(和重复的)项目添加到现有 table 的末尾,而不是覆盖它。所以它要么将新值附加到 $scope.data
,要么不会自动重做 ng-repeat
.
我该怎么做才能让它用从 Home/DataRefresh 获得的新数据覆盖 table 的内容?
这是服务器端代码的问题,它在对数据库进行新的 Get 调用时没有清除缓存。 angular 工作正常。