Cassus Cassus - 1 year ago 31
AngularJS Question

AngularJS: How to use ngOption to iterate data from cached Api call

I have an application in which I am making multiple API calls and caching that data for later. Then someone else in the app, I want to retrieve that data and list it in a drop down using ng-option.
To get the cached data for each call, I am doing

var httpCache = $cacheFactory.get('$http');
var cachedImpactedEntities = httpCache.get('my api url');

This returns an object of an array in an array like so:

[200,"[ {
"entity_id": 1,"entity_desc": "test1" },
{"entity_id": 2,"entity_desc": "test2"}]",

What would be a good way to extract just the inside array in quotes and output each "entity_desc" to the drop down using ng-option like:


The way the cached information comes back is confusing me.



If I understand you correctly the following shall be sufficient:

$scope.cachedEntities = eval(cachedImpactedEntities[1]);
<select ng-options="item as item.entity_desc for item in cachedEntities track by item.entity_id" ng-model="selected"></select>

But that means you will need to update "manually" the cachedEntities each time you get the data back from api call. Consider of using promise construct:

$http.get("url+parameters").then(function(data) { $scope.cachedEntities = data}, function(){ // do something on error });