此代码调用多个get请求并更新模型 从每个获取请求的结果。
http.get是异步的,这是否延伸到更新UI模型时?如果get请求无序返回数据(第3个get请求在第1个请求之前返回数据)则第三个值用于statusViewer指令 将在UI上首先更新?如果不是,当从get请求返回数据时,如何修改以更新UI模型?
plnkr: https://plnkr.co/edit/BjETLN7rvQ1hNRIm51zG?p=preview
plnkr src:
http-hello1.html:
{ "content" : "divContent" , "id" : "r1" }
http-hello2.html:
2. http-hello2.html
http-hello3.html:
3. http-hello3.html
index.html :
<!doctype html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="FetchCtrl">
<status-viewer ng-repeat="sourceUrl in sourceUrls" url="sourceUrl"></status-viewer>
</div>
</body>
</html>
mytemplate.html:
<!--<h1>{{url}}</h1>-->
<div>
<p>{{model}}</p>
</div>
script.js :
var myapp = angular.module('app', []).controller('FetchCtrl', FetchCtrl)
myapp.directive('statusViewer', function ($http) {
return {
restrict: 'E',
templateUrl: 'mytemplate.html',
scope: {
url: '='
},
link: function (scope, elem, attrs, ctrl) {
$http.get(scope.url).success(function (data) {
scope.model = JSON.stringify(data);
});
}
};
});
function FetchCtrl($scope, $http, $q , $parse) {
$scope.sourceUrls = [
'http-hello1.html',
'http-hello2.html',
'http-hello3.html'
];
}
更新:预期的行为是UI模型将以与调用“success”回调相同的顺序更新,我的预期行为断言是否有效?