I am trying to learn AngularJS. My first attempt to get new data every second:
'use strict'; function dataCtrl($scope, $http, $timeout) { $scope.data = []; (function tick() { $http.get('api/changingData').success(function (data) { $scope.data = data; $timeout(tick, 1000); }); })(); };
When I simulate a slow server, a sleep stream for 5 seconds, it waits for a response before updating the user interface and setting a different timeout. The problem is what I rewrote above to use Angular modules and DI to create a module:
'use strict'; angular.module('datacat', ['dataServices']); angular.module('dataServices', ['ngResource']). factory('Data', function ($resource) { return $resource('api/changingData', {}, { query: { method: 'GET', params: {}, isArray: true } }); }); function dataCtrl($scope, $timeout, Data) { $scope.data = []; (function tick() { $scope.data = Data.query(); $timeout(tick, 1000); })(); };
This only works if the server response is fast. If there is any delay, it sends out 1 request per second without waiting for a response and seems to clear the user interface. I think I need to use the callback function. I tried:
var x = Data.get({}, function () { });
but an error was received: "Error: destination.push is not a function." This was based on the docs for $ resource , but I really didn't understand the examples there.
How to make the second approach work?
javascript angularjs
David Dec 02 2018-12-12T00: 00Z
source share