$apply does not reflect $scope

Asked 1 years ago, Updated 1 years ago, 50 views

I'm using Onsen to create an app.
I'm trying to make a list in the following order.

1. Create data externally
2. Create a table with indexedDB when starting the application and store external data
3. When loading pages, store data from indexedDB into variables and hand it over to $scope
Create a list at 4.ng-repeat

I'm using $apply because I'm calling another function of 3, but the list doesn't take effect.

This function works properly.
(The list will appear in 3 seconds.)

setTimeout(function(){
    $scope.$apply(function(){
        $scope.items=[~];
    });
}, 3000);

If you use getData to import data from outside, it won't work.
However, the log successfully displays variable information.

setTimeout(function(){
    $scope.$apply(function(){
        getData()
            .then(function(value){
                $scope.items=value;
                console.log(value);
            });
    });
}, 3000);

I guess it's because the function is called in the middle, but why is it not working?
Also, is there any other good way?

Thank you for your cooperation.

monaca onsen-ui angularjs

2022-09-30 19:28

1 Answers

The function passed to then is called asynchronous, so $scope.$apply at that time is useless.
$scope.$apply must be called in then.

setTimeout(function(){
    getData().then(function(value){
        $scope.$apply(function(){
            $scope.items=value;
            console.log(value);
        });
    });
}, 3000);


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.