Javascript angular.copy()不';无法在setTimeout()中工作
我发现了这样的获取数据的示例:Javascript angular.copy()不';无法在setTimeout()中工作,javascript,angularjs,Javascript,Angularjs,我发现了这样的获取数据的示例: $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){
$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
})
这会在请求完成时更新数据,并且对服务器的请求会延迟一段时间,所以我将$http请求替换为超时,但它不起作用,没有数据更新
setTimeout(function(){
angular.copy({text : 'test'}, _data);//it doesn't update my layout
}, 100);
Angular不知道setTimeout内发生的更新。因此,您需要重新应用范围以通知Angular的更改:
setTimeout(function(){
$scope.$apply(function(){
angular.copy({text : 'test'}, _data);
});
}, 100);
理想情况下,您应该使用Angular的$timeout来摆脱$scope
谢谢,它可以工作,但是为什么它不能与setTimeout()一起工作呢?
$timeout(function(){
angular.copy({text : 'test'}, _data);
},100);