4

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

6
  • have you console data? are you getting data? Commented Jan 8, 2017 at 13:22
  • @AvneshShakya Yes, I have added an image of the console output. Can't show you the data but there is an array size. Commented Jan 8, 2017 at 13:26
  • What is the pro function? Commented Jan 8, 2017 at 14:53
  • It is a function for comparing 2 tables. It working fine. I have found problem might be within the binding between the two components. Commented Jan 8, 2017 at 15:08
  • Why are you assigning "this" to your vm variable and how come it's not declared as $scope.vm = this ? Commented Jan 8, 2017 at 15:28

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

When wrapping it in a $apply am I getting that an digest cycle already is in progress.
Are you sure its the same controller in your js and your html?
use native promise/callback mechanism ($http, $q, $resource) did the trick for me. Thanks.
0

use

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.