I have the following code in my application (stripped out otiose code so there might be typos)
Basically I need to be sure that $scope.takeovers has data before any other code is executed.
When monitoring network activity in the browser I can see that /api/pagedata is being called and successfully outputs the data I want in a $scope
However $scope.takeovers is undefined always.
Currently I have this which is not working.
app.js
var app = angular.module('app', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('index', {
url : '/',
resolve: {
takeovers: function(pageSetup) {
return pageSetup.getData().$promise;
}
},
controller: 'newFormController',
templateUrl: '/templates/form.html'
})
});
app.controller('newFormController', ['$scope', 'takeovers', function($scope, takeovers) {
$scope.takeovers = takeovers.data; //Is empty when controller is initiated...
/* continue with code that rely on $scope.takeovers */
}]);
app.factory('pageSetup', ['$http', function($http) {
var res = {
getData: function() {
var promise = $http({
method: 'GET',
url: '/api/pagedata'
});
promise.success(function(data, status, headers, conf) {
return data;
});
return promise;
}
}
return res;
}]);
/api/pagedata result
["2015-11-01","2015-11-02","2015-11-03","2015-11-08","2015-11-09","2015-11-10","2015-11-11","2015-11-15","2015-11-16","2015-11-29","2015-11-30"]
What am I missing?