简体   繁体   English

Angular.js-ng-repeat不显示更新的数组

[英]Angular.js - ng-repeat not showing updated array

I have got code that reads the data from the array perfectly when I use a AJAX request. 当我使用AJAX请求时,我得到的代码可以完美地从数组中读取数据。 When I push an object to the array however, ng-repeat doesn't render the new row and I have to refresh the page to then fetch the data that was sent to server. 但是,当我将对象推送到数组时,ng-repeat不会渲染新行,因此我必须刷新页面才能获取发送到服务器的数据。

Why does it do this? 为什么这样做呢? Thanks 谢谢

Javascript 使用Javascript

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
        })
    }

HTML HTML

<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>

That's because you're using jQuery and Angular together. 那是因为您同时使用jQuery和Angular。 Don't do that. 不要那样做 EVER. EVER。 Angular is not aware of what jQuery is doing, and jQuery is not aware of what Angular is generating in the DOM. Angular不知道jQuery在做什么,而jQuery不知道Angular在DOM中正在生成什么。 Solution : REMOVE jQuery and use Angular's own $http service. 解决方案:删除jQuery并使用Angular自己的$http服务。

The same way, don't use document.getElementById('full-fix').value . 以同样的方式,不要使用document.getElementById('full-fix').value You're taking Angular backwards. 您正在向后倾斜Angular。 Angular generates the DOM from data, so you don't need to select DOM elements to read their value because that value is already in your data. Angular从数据生成 DOM,因此您无需选择 DOM元素即可读取其值,因为该值已存在于数据中。

Update the document. 更新文档。 Use scope apply as a quick fix. 使用范围适用于快速修复。 Not the way to do it imo. 不是imo的方式。 But it will solve your problem fast. 但这将快速解决您的问题。 On my mobile if I do not forget ill update this comment later with more details and best practises. 如果我不忘记自己的手机,请稍后再更新此评论,以获取更多详细信息和最佳实践。 For now a google search to scope apply will help you a long way. 目前,适用范围的Google搜索将对您有很大帮助。

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

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