angular.copy()在setTimeout()中不起作用

时间:2021-05-07 19:40:19

I have find example of get data like this :

我找到了这样的get数据示例:

$http.get("/js/data/movies.json")
        .then(function(results){
            //Success
            angular.copy(results.data, _movies); //this is the preferred; instead of $scope.movies = result.data
        }, function(results){
            //Error
        })

This updates data when request is done and request to server delay some time, so i have replace $http request with timeout but it doesn't work, no update of data.

这在请求完成时更新数据并且请求服务器延迟一段时间,所以我用超时替换$ http请求但它不起作用,没有数据更新。

setTimeout(function(){
        angular.copy({text : 'test'}, _data);//it doesn't update my layout
    }, 100);

1 个解决方案

#1


2  

Angular is not aware of the updates that take place inside setTimeout. So you need to reapply the scope to notify Angular of the changes:

Angular不知道setTimeout中发生的更新。因此,您需要重新应用范围以通知Angular更改:

setTimeout(function(){
    $scope.$apply(function(){
        angular.copy({text : 'test'}, _data);
    });
}, 100);

Ideally you should use Angular's $timeout to get rid of that $scope.$apply()

理想情况下,你应该使用Angular的$ timeout来摆脱那个$ scope。$ apply()

$timeout(function(){
    angular.copy({text : 'test'}, _data);
},100);

#1


2  

Angular is not aware of the updates that take place inside setTimeout. So you need to reapply the scope to notify Angular of the changes:

Angular不知道setTimeout中发生的更新。因此,您需要重新应用范围以通知Angular更改:

setTimeout(function(){
    $scope.$apply(function(){
        angular.copy({text : 'test'}, _data);
    });
}, 100);

Ideally you should use Angular's $timeout to get rid of that $scope.$apply()

理想情况下,你应该使用Angular的$ timeout来摆脱那个$ scope。$ apply()

$timeout(function(){
    angular.copy({text : 'test'}, _data);
},100);