In an AngularJS controller i have created a function which makes a JSONP request. This request returns a JSONP object which has the following format.
[{
"Domain_1":{
"Notes":"This information can be usefull for generic information",
"Contacts":[
{
"ContactName":"P. rimary",
"ContactPhone":"+31 612 273 88"
}
]
},
"Domain_2":{
"Notes":"This information can be usefull for generic information",
"Contacts":[
{
"ContactName":"S. secondary",
"ContactPhone":"+31 612 273 88"
}
]
}
}];
The object is returned successfully when making the JSONP request in the following function. ($scope.method and $scope.domainName are set at scope level) What i want is to achieve is to select a specific part of the JSON data matching the value in $scope.domainName and assign this to a scope variable $scope.domainData.
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
// Only make JSONP request if domain name is known
if ($scope.domainName !== null) {
$http({method: $scope.method, url: $scope.url, timeout: 5000}).
success(function(data, status) {
$scope.status = status;
console.log("Data:") ; console.dir(data) ;
console.log("Domain Name" + $scope.domainName) ;
$scope.domainData = data[$scope.domainName] ;
console.log("DomainData: ") ;
console.dir($scope.domainData) ;
}).
error(function(data, status, headers, config) {
$scope.domainData = null ;
$scope.status = status;
console.log("$scope.fetch failed" + $scope.status + " :: " + data) ;
});
} else {
console.log("Shame... nothing to do..... ") ;
}
};
When the code is executed the Domain Name is "Domain_1" and data contains (shown in console);
0: Object
Domain_1: Object
Contacts: Array[1]
0: Object
ContactName:"P. rimary"
ContactPhone:"+31 612 273 88"
...
But $scope.domainData is undefined. How can make this selection work correctly?