我正在尝试从 JSON 响应中返回项目,但无法弄清楚语法。响应是一个自定义的 ServiceStack DTO(注意内部数组,称为“Items”):
{"Items":[{"Id":"ABC1234","Name":"Tests-TestItem01","Desc":"Test Item 01"}]}
我有一个带有方法的 AngularJS 服务getAll()
:
function getAll() {
return $http.get('http://servername.com/TestItems/GetAll')
.then(getAllComplete)
.catch(function(message) {
exception.catcher('XHR Failed for Applications')(message);
$location.url('/');
});
function getAllComplete(data, status, headers, config) {
//return data.data[0].data.results;
logger.info(data[0]);
return data[0];
}
}
我有一个控制器正在尝试使用此服务:
(function () {
'use strict';
angular
.module('testmodule')
.controller('TestModule', TestModule);
function TestModule(testItemSvc, logger) {
var vm = this;
vm.testItems = [];
vm.title = "Test Items";
vm.activate = activate;
activate();
function activate()
{
return getTestItems().then(function(){
// Log and announce
logger.info('Test Items loaded');
});
}
function getTestItems(){
return testItemSvc.getAll().then(function(data){
vm.testItems = data;
return vm.testItems;
});
}
}
})();
我可以看到返回的响应(这是我获得 JSON 的地方),但我无法确定如何返回这些项目。这就是所有摸索的地方getAllComplete()
。
编辑:
我添加了一个activate()
函数和一个 toastr 弹出窗口来显示承诺何时成功履行。它确实着火了。而且,在我的视图 HTML 中,我成功绑定到vm.title
. 但是,我ng-repeat
用来循环vm.testItems
,它从不显示任何东西。