简体   繁体   English

如何在angularjs中获取URL的ID? 我正在使用Restangular与REST API进行通信

[英]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.

相关问题 如何使用jQuery和JavaScript获取URL的特定部分? - How do I get the specific part of a URL using jQuery and JavaScript? 如果我有它的 id 值,我如何获取和更新数组中的 AngularJS 对象? - How do I get and update an AngularJS object in an array, if I have it's id value? 如何获得URL的一部分并将用户重定向到包含URL的那一部分的URL? - How do I get part of a URL and redirect the user to a URL with that part of the URL in? 当我使用AngularJS局部函数时,如何使JQuery工作? - How do get JQuery to work when I am using AngularJS partials? 如何获取随机文件的url(从目录和子目录)并将其返回给id的href? - how do i get a random file's url (from a directory & subdirectories) & return it to a id's href? 我如何通过 id 在 react native (rest api) 上获取数据 - how do i fetch data by id on react native (rest api) AngularJS:如何将数据从服务传递到控制器? - AngularJS: How do I communicate data from a service to a controller? 如何在 window.location 中的 `?` 之后获取 url 的部分 - How do I get the part of the url after the `?` in window.location 如何在URL中将#符号作为GET查询字符串的一部分传递? - How do I pass the # symbol as part of a GET querystring in the URL? 如何向最终用户隐藏 REST API Url? - How do I hide a REST API Url from the end user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM