AngularJS:如何使用 ngOption 从缓存 Api 调用中迭代数据

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

我有一个应用程序,我在其中进行多次 API 调用并缓存该数据以备后用。然后应用程序中的其他人,我想检索该数据并使用 ng-option 将其列在下拉列表中。 为了获取每次调用的缓存数据,我正在做

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

这个 returns 数组中的一个对象,如下所示:

[200,"[  {  
"entity_id": 1,"entity_desc": "test1"  },  
{"entity_id": 2,"entity_desc": "test2"}]",
{"content-type":"application/json; 
charset=utf-8"},"OK"]

什么是使用 ng-option 仅提取引号中的内部数组并将每个 "entity_desc" 输出到下拉列表的好方法,例如:

test1
test2
...

缓存信息返回的方式让我很困惑。

谢谢。

如果我对你的理解正确,以下内容就足够了:

$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>

但这意味着每次从 api 调用中获取数据时,您都需要更新 "manually" 缓存实体。 考虑使用 promise 结构:

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