I am having some problems exposing a field on my AngularJS controller. I have a webservice returning a JSON 'owner' object, with a field called 'Cave'. If this field has a null (or empty/whitespace) value, I want to hide an element in my view using ng-hide. To achieve this, I hope to expose the field 'hideCave' from the controller, shown in its entirety below:
function OwnerController($scope, OwnerFactory, $routeParams) {
if ($scope.owner == null) {
$scope.owner = OwnerFactory.getOwner($routeParams.id);
//$scope.hideCaveat = $scope.owner.Cave == undefined;
//Outputs 'Hide: {}?undefined'
alert('Hide: ' + JSON.stringify($scope.owner) + '?' + $scope.hideCaveat);
}
$scope.someButton = function () {
//Outputs full JSON string when clicked
alert('Hide: ' + JSON.stringify($scope.owner) + '?' + $scope.hideCaveat);
}
}
myApp.factory('OwnerFactory', function ($http) {
return {
getOwner: function (id) {
var url = 'api/Owner/' + id;
return $http.get(url)
.then(function (response) {
return response.data;
});
}
}
});
My data loads fine, but I can't get a value out of $scope.owner.Cave until I add a ng-click event any trigger it.
Example JSON return from web service:
{"FirstName":"David","LastName":"Lynch","Street":"Oak Dr. 7","ZipCode":"90210","Town":"Beverly Hills","Email":"","Cave":"Do not sell this man any more drugs"}
What am I doing wrong since I don't get the value?
Is my issue related to the fact I'm creating a global function for my controller? Should I use myApp.controller instead?