I have a function in my controller that checks for the day, and then depending on what time it is, switches the isOpen property of my object to either true or false. I pulled in the object like this
$http.get('js/data.json').success(function(data) {
$scope.locations = data;
});
Here is where I'm trying to change it. (i'll omit the variables I set up to save space)
$scope.checkStatus = function($scope) {
switch(day) {
case 0: $scope.locations[0].isOpen = false;
break;
case 1:
case 2:
case 3:
case 4:
case 5: if( time >= $730am && time < $9pm ) {
$scope.locations[0].isOpen = true;
} else {
$scope.locations[0].isOpen = false;
};
break;
case 6: if( time >= $11am && time < $9pm ) {
$scope.locations[0].isOpen = true;
} else {
$scope.locations[0].isOpen = false;
};
break;
default: alert("default");
};
switch(day) {
case 0: $scope.locations[0].isOpen = false;
break;
case 1:
case 2:
case 3:
case 4:
case 5: if( time >= $8am && time < $8pm ) {
$scope.locations[1].isOpen = true;
} else {
$scope.locations[1].isOpen = false;
};
break;
case 6: if( time >= $11am && time < $10pm ) {
$scope.locations[1].isOpen = true;
} else {
$scope.locations[1].isOpen = false;
};
break;
default: alert("default");
};
};
And so on for the rest of the locations.
This gives me the error "Cannot read property 'locations' of undefined." However, when I test $scope.locations[0].isOpen inside of the $http.get success function, it correctly shows me the property.
So I have two questions really: How do I access that object inside the checkStatus function? and is this the best way do do this? Is there some easier way I haven't thought about?
Edit:
I made it work by removing the checkStatus() function and just placing the switch statements inside the $http.get success function. It now looks like this. I'm not sure if this is best practice but it seems to be functional.
myApp.controller('MyController', function MyController($scope, $window, $http) {
$http.get('js/data.json').success(function(data) {
$scope.locations = data;
// variables omitted to save space
switch(day) {
case 0: $scope.locations[0].isOpen = false;
break;
case 1:
case 2:
case 3:
case 4:
case 5: if( time >= $730am && time < $9pm ) {
$scope.locations[0].isOpen = true;
} else {
$scope.locations[0].isOpen = false;
};
break;
case 6: if( time >= $11am && time < $9pm ) {
$scope.locations[0].isOpen = true;
} else {
$scope.locations[0].isOpen = false;
};
break;
default: alert("default");
}
});
I don't think you need the second $scope
here:
$scope.checkStatus = function($scope) {
Just do this:
$scope.checkStatus = function() {
The trick here is that the correct $scope
is already defined in the "scope" of this function.
Check the data that you get from your http request. your error says it can't find the object isOpen in $scope.location. Please console $scope.locations to see your nested array and then fix your code. I cannot see your json file so there isn't realy much I can do about it but try to replace all of your $scope.locations[0..1..2].isOpen = false;
with this
$scope.locations.isOpen = false;
If that doesn't fix your issue then please copy-paste the data you logged in the console or the json file so I can help you with it.