[英]Angular.js - ng-repeat not showing updated array
当我使用AJAX请求时,我得到的代码可以完美地从数组中读取数据。 但是,当我将对象推送到数组时,ng-repeat不会渲染新行,因此我必须刷新页面才能获取发送到服务器的数据。
为什么这样做呢? 谢谢
function processError() {
var responseCode = 404;
var error = {};
error["client"] = document.getElementById('client').value;
error["errorMessage"] = document.getElementById('error-message').value;
error["simpleRes"] = document.getElementById('simple-fix').value;
error["fullRes"] = document.getElementById('full-fix').value;
error["reason"] = document.getElementById('reason').value;
var errorJson = JSON.stringify(error);
$.ajax({
url: "../ErrorChecker/rest/error",
type: "POST",
data: errorJson,
contentType: "application/json"
})
.done(function (data, statusText, xhr, displayMessage) {
$('.form').hide();
responseCode = xhr.status;
reloadData(data);
});
function reloadData(data) {
if (responseCode == 200) {
processPositiveResponse(data);
} else {
$('#negative-message').show(1000);
}
}
}
function processPositiveResponse(data) {
$('#positive-message').show(1000);
updateTable(data);
$('#errorTable').DataTable().destroy();
setupTable();
clearInputs();
console.log($scope.controller.errors);
}
function updateTable(data) {
$scope.controller.errors.push({
"id": data,
"client": document.getElementById('client').value,
"errorMessage": document.getElementById('error-message').value,
"simpleRes": document.getElementById('simple-fix').value,
"fullRes": document.getElementById('full-fix').value,
"reason": document.getElementById('reason').value
})
}
<tbody>
<tr ng-repeat="x in dataCtrl.errors">
<td class="collapsing">
<div class="ui toggle checkbox">
<input type="checkbox">
<label></label>
</div>
</td>
<td style="display: none">{{ x.id }}</td>
<td>{{ x.client }}</td>
<td>{{ x.errorMessage }}</td>
<td>{{ x.simpleRes }}</td>
<td>{{ x.fullRes }}</td>
<td>{{ x.reason }}</td>
</tr>
</tbody>
那是因为您同时使用jQuery和Angular。 不要那样做 EVER。 Angular不知道jQuery在做什么,而jQuery不知道Angular在DOM中正在生成什么。 解决方案:删除jQuery并使用Angular自己的$http
服务。
以同样的方式,不要使用document.getElementById('full-fix').value
。 您正在向后倾斜Angular。 Angular从数据生成 DOM,因此您无需选择 DOM元素即可读取其值,因为该值已存在于数据中。
更新文档。 使用范围适用于快速修复。 不是imo的方式。 但这将快速解决您的问题。 如果我不忘记自己的手机,请稍后再更新此评论,以获取更多详细信息和最佳实践。 目前,适用范围的Google搜索将对您有很大帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.