It seems that despite having the same Resource object generated by two different paths, there is inconsistency in the behavior of these objects - one path allows me to retrieve the array ['values-response']['distinct-value'] successfully, while the other returns "undefined" when I attempt to do so.
The console output below follows the use of the then()
method on a promise returned by $q.all()
within a service:
Resource
$promise: Object
$resolved: true
values-response: Object
> distinct-value: Array[64]
metrics: Object
name: "term"
type: "xs:string"
Why am I unable to assign the distinct-value array to the scope for use in ng-repeat
? Previously, without using a promise, I could achieve this within the callback of a $resource
get()
method:
$scope.topics = data['values-response']['distinct-value'];
This allowed me to utilize data-ng-repeat="topic in topics"
in the template's ng-repeat
.
However, upon transitioning to a promise, if I try the following:
$scope.topics = data.topics['values-response']['distinct-value'];
the console displays "undefined", even though it appears resolved based on the previous output.
Instead, I have to do:
$scope.topics = data.topics;
Then access the array in ng-repeat
as shown below, which may not be optimized:
<button data-ng-repeat="topic in topics['values-response']['distinct-value'] | filter:textFilter | orderBy:browseOrder:browseDirection as resultsTopics" class="btn btn-default" type="button">
A MarkLogic REST API returns JSON. Although I don't believe it should be necessary, I tried using angular.fromJson()
without success.
Below is a more comprehensive look at the code with simplified snippets:
app.factory('BrowseService', [
'$q', '$resource',
function($q, $resource) {
var r = $resource('http://localhost\\:8005/LATEST/values/:facet', {
format: 'json',
options: 'browse'
});
var p1 = r.get({
facet: 'term'
});
var p2 = r.get({
facet: 'place'
});
var p3 = r.get({
facet: 'person'
});
return $q.all({
topics: p1,
places: p2,
people: p3
});
}
]);
app.controller('browseController', function($scope, BrowseService) {
$scope.browseOrder = '_value';
$scope.browseDirection = '';
$scope.textFilter = '';
$scope.people = [];
$scope.places = [];
$scope.topics = [];
BrowseService.then(function(data) {
$scope.topics = data.topics;
});
});
The following code was successful prior to returning $q.all()
from the factory:
Browse.get({
facet: 'term'
}, function(data) {
$scope.topics = data['values-response']['distinct-value'];
});
<button data-ng-repeat="topic in topics | filter:textFilter | orderBy:browseOrder:browseDirection as resultsTopics" class="btn btn-default" type="button">
Thank you for your assistance in advance.