我正在尝试遍历,以从另一个数组中的数组对象中获取所有id
和name
。即使我尝试使用以下方法从1个特定对象中抓取,也会给我“未定义”的信息:
companies[0][0]['id']
这是我的数组:
控制器:
app.controller('HomeController', ['$scope', 'companies', function($scope, companies) {
$scope.companies = companies;
console.log('companies', $scope.companies);
}]);
我什至不知道如何使用表达式在HTML中显示/访问它:
<div class="container" ng-controller="HomeController">
<div ng-repeat="company in companies" class="list">
<a href="#/{{ company.id }}" class="company-name">
{{ company.name }}
</div>
</div>
已更新
工厂:
app.factory('companies', ['$http', function($http) {
data = [];
for (let i = 1; i < 11; i++) {
$http.get('https://examplepage.com/wp-json/wp/v2/categories?per_page=50&page=' + i)
.then(function(response) {
data.push(response.data);
console.log('data', data);
},
function(err) {
return err;
});
}
return data;
}]);
答案 0 :(得分:2)
设计控制器与工厂之间的交互的方式易碎且容易出错。看到您的代码在下面注释:
// factory
app.factory('companies', ['$http', function($http) {
data = [];
for (let i = 1; i < 11; i++) {
// you CANNOT control when this is going to return
$http.get('https://examplepage.com/wp-json/wp/v2/categories?per_page=50&page=' + i)
.then(function(response) {
// so this doesn't push to `data` synchronously, this DOES NOT
// guarantee you when you return data, every response.data will be there
data.push(response.data);
console.log('data', data);
},
function(err) {
return err;
});
}
// this will always be returned empty ([], as you initialized it) because
// the async responses (commented above) haven't arrived when this lint his hit.
return data;
}]);
// controller
$scope.companies = companies; // so, companies will always be [] (empty array)
您应该坚强考虑将实现工厂的方式更改为以下方式:
想法:
x
(11)次以获取550个项目(50 * 11次)getCompanies
和itemsPerPage
参数的函数(page
),这样您就可以根据需要在页面中获取尽可能多的项目。即:要获取550件商品,您应该将其命名为:companies.getCompanies(550);
companies.getCompanies
代码:
// factory
app.factory('companies', ['$http', function($http) {
function fnGetCompanies(itemsPerPage, page) {
var ipp = itemsPerPage || 50; // 50 default
var page = page || 0; // 0 default page
// return the promise instead of data directly since you cannot return the value directly from an asynchronous call
return $http
.get('https://examplepage.com/wp-json/wp/v2/categories?per_page=' + ipp + '&page=' + page)
.then(
function(response) {
// and then return the data once the promise is resolved
return response.data;
},
function(err) {
return err;
}
);
}
// provide a `getCompanies` function from this factory
return {
getCompanies: fnGetCompanies
}
}]);
// controller
// get 550 items starting from 0
companies.getCompanies(550, 0).then(function(companies) {
$scope.companies = companies;
});
记住You cannot return from an asynchronous call inside a synchronous method
您可以先使用Array.prototype.reduce来将多数组结构转换为单个数组,如下所示:
$scope.companies = companies.reduce(function(prevArr, currentArr) { return prevArr.concat(currentArr);}, []);
这将转换如下结构:
[
[{id: 1, name: 'A'}, {id: 2, name: 'B'}, {id: 3, name: 'C'}],
[{id: 4, name: 'D'}, {id: 5, name: 'E'}, {id: 6, name: 'F'}]
];
对此:
[{"id": 1,"name": "A"},{"id": 2,"name": "B"},{ "id": 3,"name": "C"},{"id": 4,"name": "D"},{"id": 5,"name": "E"},{"id": 6,"name": "F"}]
简单演示:
var companies =[
[{id: 1, name: 'A'}, {id: 2, name: 'B'}, {id: 3, name: 'C'}],
[{id: 4, name: 'D'}, {id: 5, name: 'E'}, {id: 6, name: 'F'}]
];
console.log(companies.reduce(function(prev, current) { return prev.concat(current);}, []));