user3316920
user3316920

Reputation:

AngularJS ng-repeat not refreshing on array update

I have a problem where the ng-repeat aren't updating the list when the array change.

I'm using a JavaScript promise to compute a calculation to then return an object containing 2 arrays. These arrays is then to be shown on the view on the below code snippet.

<button class="btn" data-toggle="modal" data-target="#tableModal" ng-click="vm.compareTables(vm.table)">Some text</button>
<!-- Modal -->
<div class="modal fade" id="tableModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">{{ vm.table.title}}</h4>
            </div>
            <div class="modal-body">
                <h4>You got</h4>
                <p ng-repeat="g in vm.got">{{ g }}</p>
                <h4>You dont:</h4>
                <p ng-repeat="d in vm.dont">{{ d }}</p>
            </div>
        </div>
    </div>
</div>

In the controller am I wrapping a function in a promise and it returns the right answer with 2 filled arrays. I can also print the result in the controller, the ng-repeat are just not updating the view.

var vm = this;

vm.compareTables = function (table) {
    getData().then(function succesCallback(response) {
        vm.ing = response.data;
        var r = table;
        var promise = pro(r, vm.ing);
        promise.then(function (data) {
            console.log(data.got);
            console.log(data);
            vm.got = data.got;
            vm.dont = data.dont;
        });
    });
}

The above promise returns the right result.

I'm using angular 1.6.1 if that helps. The controller and view is connected to a component, which is working fine.

UPDATE

Here is the console output for the promise return Console output

Upvotes: 4

Views: 2455

Answers (4)

r0ck
r0ck

Reputation: 173

when your out of angular context you need to use $scope.$apply() method in promise callback to update your view.

Upvotes: 0

Vivek Verma
Vivek Verma

Reputation: 303

I hope this helps you . Refer this link and if not clear watch the video given at the end of this link . solve problem of ng-repeat

Upvotes: 0

Norbert Huurnink
Norbert Huurnink

Reputation: 1316

Wrap your vm.got = .. in a $scope.$apply(function () { ... }) to force a digest cycle. Angular is probably not aware of your promise callback because you probably use a different promise/callback mechanism than angular native ones ($http, $q, $resource).

Upvotes: 2

user3945261
user3945261

Reputation:

use

$timeout(function () {
    vm.got = data.got;
    vm.dont = data.dont;
})

Upvotes: 0

Related Questions