I am struggling to retrieve certain information in advance. Specifically, I need to access a variable called var myId
and pass it to myapp.run()
. In my setup, there are 2 controllers each handling different pages.
var myId; // This variable is crucial
afroEarthApp.controller('afroEarthMainCtrl',['$scope','$http','$location','$cookies', '$rootScope', function($scope, $http, $location, $cookies, $rootScope) {
$http.get('js/afro.json').success(function(data) {
$scope.myDataFirst = data;
$scope.singleNiche = $scope.myDataFirst.US;
$rootScope.header = $scope.singleNiche;
$rootScope.myDataVar = $scope.myDataFirst.US;
});
$scope.setCountry = function (choise) {
switch (choise){
case "US" : $scope.singleNiche = $scope.myDataFirst.US;
break;
case "UK" : $scope.singleNiche = $scope.myDataFirst.UK;
break;
case "SA" : $scope.singleNiche = $scope.myDataFirst.SA;
break;
case "CAN" : $scope.singleNiche = $scope.myDataFirst.CAN;
break;
case "AU" : $scope.singleNiche = $scope.myDataFirst.AU;
break;
}
$cookies.put("myCountry", choise);
$rootScope.header = $scope.singleNiche;
};
$rootScope.bodylayout = "home-page";
myId = $scope.singleNiche // assigning here
}]);
afroEarthApp.controller('SingleCtrl',['$scope','$http', '$location', '$routeParams', 'Single', '$cookies', '$rootScope', function($scope, $http, $location, $routeParams, Single, $cookies, $rootScope) {
$scope.niche = $routeParams.niche;
$rootScope.bodylayout = "single-page";
var url = 'sites/'+$routeParams.niche+'.json';
Single.get({niche: $routeParams.niche}, function (data) {
$scope.singleFirst = data;
$scope.singleNiche = $scope.singleFirst.US;
var getCountry = String($cookies.get("myCountry"));
$scope.setCountry = function (choice) {
switch (choice) {
case "US" :
$scope.singleNiche = $scope.singleFirst.US;
break;
case "UK" :
$scope.singleNiche = $scope.singleFirst.UK;
break;
case "SA" :
$scope.singleNiche = $scope.singleFirst.SA;
break;
case "CAN" :
$scope.singleNiche = $scope.singleFirst.CAN;
break;
case "AU" :
$scope.singleNiche = $scope.singleFirst.AU;
break;
}
$rootScope.header = $scope.singleNiche;
}
$scope.setCountry(getCountry);
})
$rootScope.header = $scope.singleNiche;
myId = $scope.singleNiche; //assigning here
}]);
afroEarthApp.run(function ($rootScope) {
$rootScope.$on('$viewContentLoaded', function () {
...some code...
console.log(myId) // undefined
console.log(myId.id) // undefined
...some code...
});
});
I could really use some assistance with this situation.