javascript - AngularJS - polling into persistent data store -
Angular knot here. I am creating that app for which every other URL has to be selected, and it is to continuously store that data, because it needs to be accessed by multiple views / controllers.
What have I done to handle it, my http request in the factory, and data is supplied to the data controller in the factory. The issue I am having now is that the factory function has been encoded before the http request, which can cause errors in my app.
Here is the code:
App.factory ('Metrics', function ($ http) {var service; var user = [{laps: []}] UpdateMetrics = function () {// updates the user array in the factory}; $ http.get ('Data.csv'). Success (work) {var temp_array = data.split (""); Updated metrics ( 0, temp_array);}); service.lastLapInfo = function (for var lastlap = []; (var i = 0; i & lt; users.length; i ++) {var leap = user [i] Omissions [user [i] .laps.length-1]; last lump. Pash (lap);} finally return;}; return service;}); App.controller ('main controller', function ($ radius, $ http, metrics) {$ scope.users = metrics.lastLapInfo ();}); lastLapInfo () is being called before the http request, which is the cause of errors because there is no data in the array. any idea?
In addition to this - if I usually go wrong to fulfill my use case (like I should use something instead of a factory) tell me! <<< P>
This is a common use case for promise , or angular.
The following approach uses a factory that promises:
App.factory ('Metrics', function ($ http) {var service ... ... service.load = function () {return $ Http.get ('data.csv');}); Return service; }); In the controller you call your metric service, and you use the promise:
App.controller ('mainController', function ( Until $ (function (feedback) {// At this point you know for sure that the request has been terminated / you can call the logic again to run the last leapinfo Are here.});});
Comments
Post a Comment