angularjs - UI not updating when property changed within $interval callback -
i have service defines background $interval. watches local storage see if server updates didn't made due connectivity problems. when finds some, periodically tries contact server updates, deleting them local storage when succeeds.
upon success, callback updates properties of angular view/model. changes should cause ui update, , elsewhere in code of various controllers do.
but within $interval callback in background service changes not cause ui update. thought might kind of failure $apply, added $rootscope.$apply() call. caused error because $digest in progress.
that tells me $digest running after callback executes -- would've expected -- it's not causing ui update.
i did $rootscope.$emit() within service, after view/model updated, , listened event within controller ui should've updated. within listener can see view/model controller based on updated service.
here service:
app.factory('bkgndupdates', function($interval, $q, $rootscope, datacontext, localstorage) { var _interval = null; var _service = { isrunning() { return _interval != null }, }; _service.start = function() { if( _interval != null ) return; _interval = $interval(function() { // check ensure local storage available // , has visits process var visits = localstorage.visits; if( !visits || (visits.length == 0) ) return; var recorded = []; var promises = []; var offline = false; for( var idx = 0; idx < visits.length; idx++ ) { var visit = visits[idx]; promises.push(datacontext.dovisitaction("/walk/recordvisit", visit) .then( function(resp) { offline &= false; var home = datacontext.findhomebyid(visit.addressid); if( home != null ) { // these values should cause map icon switch yellow blue // elsewhere in app that's happens...but not here. home.visitinfo.isdirty = false; home.visitinfo.indatabase = true; home.visitinfo.candelete = true; home.visitinfo.followupid = resp.followupid; } recorded.push(visit.addressid); }, function(resp) { // went wrong; nothing, item // in local storage offline |= true; }) ); } $q.all(promises).then( function(resp) { for( var idx = 0; idx < recorded.length; idx++ ) { localstorage.removevisitbyid(recorded[idx]); } if( !localstorage.hasvisits ) { $interval.cancel(_interval); _interval = null; } $rootscope.$emit("visitupdate"); }, function(resp) { alert("some promise failed"); }); }, 30000); } _service.stop = function() { $interval.cancel(_interval); _interval = null; } return _service; }); here's controller map:
app.controller("mapctrl", function ($scope, $rootscope, $location, datacontext) { $scope.datacontext = datacontext; $scope.mapinfo = { center:datacontext.center, zoom: datacontext.zoom, pins: datacontext.pins, }; $scope.$on('mapinitialized', function(evt, map){ $scope.dragend = function() { $scope.datacontext.center = $scope.map.getcenter(); $scope.datacontext.getmapdata($scope.map.getbounds()); } $scope.zoomchanged = function() { $scope.datacontext.zoom = $scope.map.getzoom(); $scope.datacontext.getmapdata($scope.map.getbounds()); } $scope.datacontext.getmapdata($scope.map.getbounds()) .then( function() { $scope.mapinfo.pins = $scope.datacontext.pins; }, function() {}); }); $scope.click = function() { datacontext.pinindex = this.pinindex; if( datacontext.activepin.homes.length == 1) { datacontext.homeindex = 0; $location.path("/home"); } else $location.path("/unit"); }; $rootscope.$on("visitupdate", function(event) { // added next line force update...even though // data on both sides of assignment same (i.e., // changed $scope.mapinfo.pins = $scope.datacontext.pins; event.stoppropagation(); }); }); here's (partial) template (which relies on ngmap):
<div id="mapframe" class="google-maps"> <map name="themap" center="{{mapinfo.center.tourlvalue()}}" zoom="{{mapinfo.zoom}}" on-dragend="dragend()" on-zoom_changed="zoomchanged()"> <marker ng-repeat="pin in mapinfo.pins" position="{{pin.latitude}}, {{pin.longitude}}" title="{{pin.streetaddress}}" pinindex="{{$index}}" on-click="click()" icon="{{pin.icon}}"></marker> </map> </div> so why isn't ui updating?
Comments
Post a Comment