This question has already been answered:
Help please improve the script.
There is a json file. I load it and I need to determine exactly when the data came. Here is a sample code. His weak point is that verification:
if($scope.news) { console.log('OK'); } else { console.log('NOTOK'); }; displays incorrect information ("NOTOK") to the console while data is successfully received.
To solve the problem, I tried to wrap this code in $ timeout (in the same example, the corresponding lines are commented out).
But this is a bad practice because I set the interval to 1000ms, and when I receive a json file (for example, from a server) a delay can occur and this interval will be exceeded.
On the other hand, if the json file is received from the server in less than 1000ms, the user will look too much at a screen where nothing happens
In general, please help rewrite the code humanly.
I give the full code:
var app = angular.module('plunker', []); app.controller('MainCtrl', ['$rootScope', '$scope', '$timeout', '$http', function($rootScope, $scope, $timeout, $http) { $scope.getNews = function() { $http.get('news.json') .then(function successCallback(resp){ console.log('success', resp); console.log('data', resp.data); console.log('status', resp.status); console.log('statusText', resp.statusText); $scope.news = resp.data.values; }, function errorCallback(resp) { console.log('error', resp); console.log('data', resp.data); console.log('status', resp.status); console.log('statusText', resp.statusText); }); }; $scope.getNews(); //$timeout(function() { if($scope.news) { console.log('OK'); } else { console.log('NOTOK'); }; //}, 1000); }]);