I am trying to find a way to minimize jsonp requests as angular does not cache these. My approach is to set the empty arrays I had in each controller into a main controller that loads once as it is attached to body. After that I will just check if the array is empty and if it is then use the service and return the json. My problem now is that for some reason that I do not understand the array is empty when checked outside of the service call.
example code
angular.module('Feed.controllers', [])
.controller('MainCtrl', function($scope)
{
function getYear()
{
var currentDate = new Date();
return currentDate.getFullYear();
}
$scope.currentYear = getYear();
$scope.StandingsList = [];
})
.controller('StandingsCtrl', function($scope, APIservice, $routeParams)
{
console.log($scope.StandingsList.length); // returns 0
if ($scope.StandingsList.length === 0)
{
APIservice.getSomtehing().success(function (response)
{
$scope.StandingsList = response.jsonData;
console.log($scope.StandingsList.length); // returns total entries
});
};
console.log($scope.StandingsList.length); // returns 0
})
MainCtrl is in body tag as I am using the currentYear to various places in HTML StandingsCtrl is called using routes and partials for presentation. The div with the ng-view is inside the body element of MainCtrl
$scope, so you are not pointing to the same variablescopevariables, only when those arerootScopevariables, both controllers will inherit them, as allscopesare descendants of therootScope