In Angular, you do not wait for data before rendering. You allow rendering, even if the array is empty first, and then, as soon as the data returns, you will render again. Angular does this to maintain a high level of responsiveness. This is by design, not what you should try to change.
All you need to do to fix your undefined variable is initializing the scope stations variable in your controller. Although it is overwritten when data is returned from your service, it does not matter, since Angular will keep track of changes in the scope variable by reference and will update all views when that happens.
angular.module('starter.controllers', []) .controller('AppCtrl', function($scope, $http) { $scope.webservice_url = "http://webserviceurl.com/"; $scope.stations = []; $http.get($scope.webservice_url+"?action=get_settings").success(function(data, status, headers, config) { $scope.stations = data.stations; }); })
In your internal controller, if the data has not returned yet, $ scope.stations will be an empty array:
.controller('PlaylistCtrl', function($scope, $stateParams) { console.log($scope.stations); // is [] $scope.$watch('stations', function(newVal) { alert('data returned!! updating views!'); }); })
As soon as the data is returned, the array reference in the area will be overwritten, any $ watch handler is called to update the view.
source share