如何设置角j的动态网址 [英] How to set dynamic url for angular js
问题描述
我的地址栏网址是
hostname / admin / Usergroup_controller / view_members / 1
我使用angular js在内部调用
hostname / admin / Usergroup_controller / get_view_members / 1
这里的最后是id,现在是1,
$ http.get(site_url +'hostname / admin / Usergroup_controller / get_view_members / 1')。success(function(data){}
我如何???
希望你明白
根据您的问题,我了解您想要在浏览器网址中包含id,例如 hostname / admin / Usergroup_controller / view_members / 1
从而希望使用 $ http.get(site_url +'hostname / admin / Usergroup_controller / get_view_members / 1')中的'id'执行GET请求
我假设你使用 $ routeProvider
(Angular Routing)。现在这意味着你需要调整 .when(path,route)
方法在浏览器的url中传递动态的'id'。所以, .when(path,route)
将像 .when('/ Usergroup_controller / view_members /:id',templateUrl:'viewMembers。 html')
;假设 viewMembers.html
是您要路由到的网页。
之后,将 .when(path,route)
,您将需要从浏览器的URL中提取:id
的值。这将通过在路由页面的控制器中使用 $ routeParams
来完成,在这种情况下将 viewMembers.html
。在 viewMembers.html
的控制器中,您需要通过写入以下内容来提取:id
的值:
$ scope.idEnteredInBrowserUrl = $ routeParams.id;
现在,您需要传递值 $ scope.idEnteredInBrowserUrl
在您将执行 $ http.get()
请求的服务中。这可以通过在服务中创建一个函数来实现,例如
var setIdEnteredInBrowserUrl = function(id){
$ http.get(site_url +'hostname / admin / Usergroup_controller / get_view_members /'+ id).success(function(data){}}
请注意,在setter函数中提到了 $ http.get()
,因为你想要 $ http。 get()
只有在此方法被调用后才能被调用,并且您必须将:id
值附加到GET请求URL。 / p>
从 viewMembers.html
的控制器调用此方法,并将提取的:id
值, $ scope.idEnteredInBrowserUrl
到服务。
My address bar url is
hostname/admin/Usergroup_controller/view_members/1
I am calling internally using angular js
hostname/admin/Usergroup_controller/get_view_members/1
Here last is id which is now 1 but can be change.
I want to pass that url in following code
$http.get(site_url + 'hostname/admin/Usergroup_controller/get_view_members/1').success(function (data) {}
How can I ???
Hope you understand
From your question, I understand that you want to include the 'id' in the browser url, like hostname/admin/Usergroup_controller/view_members/1
and thereby want to perform a GET request using that 'id' as in $http.get(site_url + 'hostname/admin/Usergroup_controller/get_view_members/1')
I am assuming that you are using $routeProvider
(Angular Routing). Now that means that you need to tweak the .when(path, route)
method to pass the dynamic 'id' in the browser's url. So, the .when(path, route)
will be like .when('/Usergroup_controller/view_members/:id', templateUrl: 'viewMembers.html')
; assuming viewMembers.html
is the page you want to route to.
After, changing the .when(path, route)
you will need to extract the value of :id
from the browser's url. That will be done by using $routeParams
in the controller of the routed page which in this case will be viewMembers.html
. In the controller of viewMembers.html
you need to extract the value of :id
by writting this:
$scope.idEnteredInBrowserUrl=$routeParams.id;
Now, you will need to pass the value, $scope.idEnteredInBrowserUrl
in the service from which you will be performing the $http.get()
request. This can be done by creating a function in the service, say
var setIdEnteredInBrowserUrl = function (id) {
$http.get(site_url + 'hostname/admin/Usergroup_controller/get_view_members/'+id).success(function (data) {}}
Please note that the $http.get()
is mentioned inside the setter function because you want the $http.get()
to be called only after this method has been called and you have to :id
value to append it to the GET request URL.
Call this method from the controller of the viewMembers.html
and pass the extracted :id
value, $scope.idEnteredInBrowserUrl
to the service.
这篇关于如何设置角j的动态网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!