我是 Angular 新手,但设法进行 Ajax 调用并在 ListView 中从随机用户生成器 API 打印出用户。 现在我想在单击用户时进行详细 View 。 在我的 HTML 中,我进行了一个函数调用: fetchInfoById(user.id.value) 在我的脚本中,函数:
$scope.fetchInfoById = function(info_id) {
$http.get("https://randomuser.me/api/?id.value="+info_id)
//also tried: $http.get("https://randomuser.me/api/?id/value="+info_id)
.success(function(data) {
$scope.oneUserResult = data.results;
});
}
它确实为我提供了详细 View 的用户,但不是所选的 View 。我究竟做错了什么?
最佳答案
感谢您的好建议。 我知道它是一个随机生成器,但将请求的参数设置为:“seed=...”,每个 ListView 请求上都会显示相同的人:
$http.get('https://randomuser.me/api/?results=15&seed=abc&inc=gender,name,location,email,dob,phone,cell,id,picture,info,nat&nat=gb')
.success(function(response){
$scope.userResult = response.results;
});
然后,我获取每个人的 ID,并将其作为参数传递给详细 View 请求的函数调用。
我尝试使用console.log()来确保我为详细 View 请求传递了正确的值,然后甚至对 请求的参数即:
$scope.getInfoById = function(info_id) {
console.log("from HTML: "+info_id.value ); // = JK 00 46 67
$http.get("https://randomuser.me/api/?id="+'JK 00 46 67 H') ...
API 背后的 jason 数据的 id 属性格式如下:
{
"results": [
{
"id": {
"name": "BSN",
"value": "04242023"
},...
我还没弄清楚如何通过 id 获取单个用户。始终获得不同的用户,即使使用硬编码的 ID... 我的解决方案不是发出第二个请求,而是将“单击的用户”作为详细 View 的参数传递。
关于javascript - 随机用户生成器 API - 通过 ID Angular JS 获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42907146/