我假设您有一个 ajax 调用来填充这些数据集,因此我在这里使用超时来模拟 ajax 承诺。这就是我会按照你的要求做的:
function myService($timeout) {
var data1 = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"],
data2 = [ 'xx','xxx','xxxx'];
return {
returnData : function() {
return $timeout(function() {},200).then(function() {
return [].concat(data1,data2);
});
}
}
}
function DefaultCtrl($scope, myService) {
myService().then(function(response) {
$scope.names = response;
});
}