What happens if you initialize lastReportedEvents to an empty array instead of null?
Kevin On Thu, Dec 18, 2014 at 9:22 AM, Rob Parker <[email protected]> wrote: > > Hi, > > I have not long started out using AngularJS, and I am having a problem > getting some data returned in a $http request on a service I have written > to be used on a $scope property defined in one my controllers. > > My service looks like so: > > app.factory('DataStoreService', function ($rootScope, $location, $http) { > var lastReportedEvents = null; > //For our inital request to server, but for subsequent requests we should > use the nextURL > //property of the previous successful response > var locationStoreURL = '/location/GetLastReportedEvents.json'; > > return { > > lastReportedEvents: lastReportedEvents, > getLastReportedEvents: function () { > $http.get(locationStoreURL) > .success(function (data, status, headers, config) { > > if (data.success) { > lastReportedEvents = angular.fromJson(data).data; > > //TO-DO, use the nextURL property on the JSON > response and set locationStoreURL var in this service > > } > else { > console.log('getLocationData ERROR!'); > } > }) > .error(function (data, status, headers, config) { > console.log('getLocationData ERROR!'); > }); > > } > > > }; > }); > > > When the user logs in to the application I am making the request for data > in the app run block like so. > > app.run(['$rootScope', '$location', 'DataStoreService', function ($rootScope, > $location, DataStoreService) { > > > $rootScope.$on('login', function (event) { > //console.log('login event detected'); > DataStoreService.getLastReportedEvents(); > }); > > }]); > > > In my controller I have: > > app.controller('LocationsController', ['$rootScope', '$scope', > 'DataStoreService', function ($rootScope, $scope, DataStoreService) { > > $scope.locations = DataStoreService.lastReportedEvents; > }]); > > > So as you can see, I'm just trying to assign a property of my service that > represents the data returned from the $http request to the $scope on my > controller that my view is bound to: > > <div ng-controller="LocationsController"> > > <table class="table table-hover table-condensed"> > <tr ng-repeat="location in locations"> > <td> > <span>{{location.UnitName}}</span> > <p>{{location.Location}}</p> > </td> > </tr> > </table ></div> > > Nothing appears in my view. I can get it to work by emitting an event in > the success handler of my request and then assigning to the $scope in my > controller in an event listener, but this seems a bit backwards to me. I > thought by injecting a service to a controller, that any changes to the > service (properties, etc) are broadcast to any controllers using that > service, or am I mistaken. > > I must be missing something fundamental here! > > Thanks > > -- > You received this message because you are subscribed to the Google Groups > "AngularJS" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
