我在$http.get
请求中收到了一个对象数组。我做了以下内容:如何从Angular中的节点返回的对象数组中访问元素?
$http.get("/showdata").then(function (response) {
var thedata = response.data.category;
console.log(thedata);
$scope.alldata = response.data;
if (thedata === "school") {
$scope.category = "SchoolBC";
} else {
$scope.category = "Not School";
}
});
我怎么能在响应检查一番,并设置相应的$scope
?
我得到的回报是:
[
{
id: "123456",
category: "school",
title: "first test"
},
{
id: "789012",
category: "home",
title: "second test"
}
]
在前端,我有:
<ul ng-repeat = "mydata in alldata">
<li>{{mydata.title}}<p>{{category}}</p></li>
</ul>
这是一个数组。所以你使用response.data [0]访问它的第一个元素,使用response.data [1]访问它的第二个元素。 –
它不清楚你在问什么,但似乎你需要使用for循环,或forEach(假设你有更多的数据,而不仅仅是这些) –
我怎么能把一个forEach循环放在一个'$ http。得到'? – Somename