我敢肯定这已经被问过很多次了,但是我……找不到。现在是晚上 10 点 30 分,我必须把这个留给你们!
本质上,我有两次调用 json 服务器方法——更新数据并获得新的总数。从逻辑上讲,首先调用更新数据,通过 $emit 发送通知,获取新的总数。不幸的是,当我在服务器控制器上设置断点时,在 SetAlertRead 之前调用了 GetAlertTotals,因此返回了错误的数据。
'totals' 代码在另一个控制器中,因此 $emit/publish/subscribe:
$scope.$on('RefreshUnreadItemsCounter', function () {
$scope.alertCount = $scope.GetAlertCount();
$scope.alertTotals = $scope.GetAlertTotals();
$scope.$apply();
});
数据位于 uiGrid 中,具有以下(部分)定义:
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.EDIT);
$scope.gridApi.selection.on.rowSelectionChanged($scope, function (row) {
var authorisationToken = $('input[name="__RequestVerificationToken"]').val();
row.entity.AlertSeen = true;
$scope.message = row.entity.Message;
$http({
url: '/Alerts/SetAlertRead',
contentType: "application/json; charset=utf-8",
method: 'POST',
data: { rowId: row.entity.UserAlertsId },
headers: { '__RequestVerificationToken': authorisationToken }
});
$scope.$emit('RefreshUnreadItemsCounter');
});
用户单击一行以显示与该行关联的数据并“读取”它,通过 json 调用 ( /Alert/SetAlertRead
) 触发数据更新。我假设我在 $emit 调用之前设置了一个超时,以给承诺时间来执行 json 调用,但我不确定如何。
请帮忙!如果您不介意,我将需要尽可能多的代码!再次感谢。