[英]How do I get an ID that is part of a URL, in angularjs? I am using Restangular to communicate with REST API's
I am using Restangular to make backend calls and everything works fine as long as URL gives me data like below: 我正在使用Restangular进行后端调用,并且只要URL提供如下数据,一切都可以正常工作:
If I hit http://mysite/responses/
the responses look like 如果我点击
http://mysite/responses/
则响应看起来像
[
{"fname":"some guy",
"lname":"some name",
"dob":2091,
"job":"mgr",
"id":"100"
},
{"fname":"another guy",
"lname":"anpther name",
"dob":1991,
"job":"worker",
"id":"101"
},
{"fname":"someone",
"lname":"some name",
"dob":1997,
"job":"dev",
"id":"102"
},
{"fname":"some guy2",
"lname":"some name",
"dob":2091,
"job":"sec",
"id":"103"
}
]
and everything works fine 一切正常
which is wrapped in an array and my Restangular URL looks like 它包装在一个数组中,我的Restangular URL看起来像
RestangularProvider.setBaseUrl('http://mysite/responses/');
RestangularProvider.setRestangularFields({ id: 'id' });
and it works all fine 而且一切正常
Now I deconstructed the backend and API returns the data in below format 现在,我解构了后端,API以以下格式返回了数据
http://mysite/responses/100
{
fname:"some guy",
lname:"some name",
dob:2091,
job:"mgr",
id:"100"
}
http://mysite/responses/101
{
fname:"another guy",
lname:"another name",
dob:1991,
job:"sec",
id:"101"
}
http://mysite/responses/102
{
fname:"some name",
lname:"some person",
dob:1091,
job:"sec",
id:"102"
}
http://mysite/responses/103
{
fname:"a guy",
lname:"person name",
dob:1998,
job:"dev",
id:"103"
}
Now if I hit 现在如果我打
http://mysite/responses/
I get below responses 我得到以下回应
http://mysite/responses/100
http://mysite/responses/101
http://mysite/responses/102
http://mysite/responses/103
http://mysite/responses/104
As you see, the id is getting appended to the url, and I need to get this id, make an Restangular call and get the data. 如您所见,该ID会附加到网址中,我需要获取此ID,进行一次Restangular调用并获取数据。 How can I loop through this multiple http calls, get the id and put it in Angular.
我如何遍历这多个http调用,获取id并将其放在Angular中。 I think I am not constructing the BaseURl for Restanglar properly, because I am not sure how to get the id.
我认为我没有为Restanglar正确构建BaseURl,因为我不确定如何获取ID。 thanks for the help
谢谢您的帮助
What you are looking for is routeParams
: 您正在寻找的是
routeParams
:
https://docs.angularjs.org/api/ngRoute/service/ $routeParams https://docs.angularjs.org/api/ngRoute/service/ $ routeParams
This is used together with routes: 这与路线一起使用:
https://docs.angularjs.org/api/ngRoute/service/ $route https://docs.angularjs.org/api/ngRoute/service/ $ route
For example: 例如:
$routeProvider.when('/Book/:bookId', {
templateUrl: 'book.html',
controller: function($scope, $routeParams) {
console.log($routeParams.bookId);
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.