【发布时间】:2013-05-19 18:37:43
【问题描述】:
我是 AngularJS 的新手,即使花了几个小时我也无法在互联网上找到以下问题的答案。
请随时提出更好的方法,我在下面尝试。
查看我的 AngularJS 代码:
<li ng-repeat="result in getResults()" ng-controller="resultController">
<div class="name_style">
{{result.name}}
</div>
<!--fetch more details for each result and set to $scope.resultDetail-->
<div class="name_detail_style" ng-bind=getDetails(result.name) ng-controller="resultDetailController">
{{resultDetail.image}}
{{resultDetail.description}}
</div>
</li>
为简洁起见,我试图在上面进行简化。
我正在尝试分两部分呈现结果列表:
getResuls() 调用休息服务以获取结果数组。
然后 getDetails(result) 进行另一个服务调用以获取特定结果的更多详细信息。
它运行得非常慢,因为 AngularJS 试图同时渲染名称和它的细节。我希望名称在可用时立即呈现,并在 getDetail() 服务调用有机会获得响应时呈现详细信息。
我不知道是否/如何使用此解决方案 How to load data asynchronously using AngularJS with SpringMVC?
如果您也需要查看控制器端代码,请告诉我。
EDIT 也添加控制器代码示例。请原谅手动重新创建以下代码中的任何拼写错误。
function resultController($scope, $http) {
$scope.getResults = function() {
$http({method: 'GET', url: resultURL= timeout: maxTime})
.success(function (data, status, headers, config) {
console.log(data);
$sope.result=data;
})
.error(function (data, status, headers, config) {
if (status == 0) {
serviceTimedoutError($scope, data, status, config);
} else {
serviceFailure($scope, data, status, config);
}
})
};
};
function resultDetailController($scope, $http) {
$scope.getDetails = function(result) {
resultDetailsURL=resultDetailsUR+"/"+result
$http({method: 'GET', url: resultDetailsURL= timeout: maxTime})
.success(function (data, status, headers, config) {
console.log(data);
$sope.resultDetails={"image":data["image"],"description":data["description"]};
})
.error(function (data, status, headers, config) {
if (status == 0) {
serviceTimedoutError($scope, data, status, config);
} else {
serviceFailure($scope, data, status, config);
}
})
};
};
【问题讨论】:
-
您将模型直接设置为函数调用,这并不典型。 (我以前没见过这个)。大多数情况下,您会看到
ng-repeat = "result in results",其中results实际上是在$scope.results = getResults();等控制器中定义的。但是,您也应该发布一些控制器代码或工厂/服务代码,以便获得完整的答案。 -
好的,也要发布控制器代码。
标签: javascript web-services twitter-bootstrap angularjs