Asp.net angularjs从url获取参数
我目前正在一个asp.NETMVC站点上工作,在该站点中,我们使用angularjs进行模型绑定。我有一个控制器设置,但我需要从url获取id以将其传递给服务。我的url看起来像:Asp.net angularjs从url获取参数,asp.net,angularjs,asp.net-mvc,Asp.net,Angularjs,Asp.net Mvc,我目前正在一个asp.NETMVC站点上工作,在该站点中,我们使用angularjs进行模型绑定。我有一个控制器设置,但我需要从url获取id以将其传递给服务。我的url看起来像: http://localhost/myapp/section/5 我需要抓取url中的5个,我们没有使用angular进行路由,是否有任何方法可以通过angular抓取?否则,我可以使用.net将其注入全局js变量,并使用angular从中读取id 我按如下方式设置角度控制器: myModule.contr
http://localhost/myapp/section/5
我需要抓取url中的5个,我们没有使用angular进行路由,是否有任何方法可以通过angular抓取?否则,我可以使用.net将其注入全局js变量,并使用angular从中读取id
我按如下方式设置角度控制器:
myModule.controller('SectionController', ['$scope', 'sectionRepository', '$routeParams', SectionController]);
function SectionController($scope, sectionRepository, $routeParams) {
var vm = this;
alert($routeParams.id);
警报返回“undefined”(未定义),我假设,因为我从未在angular中设置路由,有没有一种不设置路由的方法,因为我们不想使用angular进行路由。您可以使用
$location
服务获取URL。从那里,只需解析它
function SectionController($location) {
var url = $location.url();
//regex is slow, you should use substring/slice instead
//var regex = /(?:section\/)[0-9]+/;
var id = url.substring(url.indexOf("section/") + "section/".length);
alert(id);
}
您可以使用
$location
服务获取URL。从那里,只需解析它
function SectionController($location) {
var url = $location.url();
//regex is slow, you should use substring/slice instead
//var regex = /(?:section\/)[0-9]+/;
var id = url.substring(url.indexOf("section/") + "section/".length);
alert(id);
}
可能使用$location.absUrl()并解析字符串可能使用$location.absUrl()并解析字符串如果angular没有路由应用程序,是否确定$location.url()可以工作?@SergioMarron根据文档(),它不依赖于路由。
window.location
中的更改是活动的,$location
可以看到它们。我尝试了$location.path(),但它返回了一个空字符串。window.location.pathname起作用。@SergioMarron for me$location.url()也不起作用,但在查看调试器后,我发现有$location.absUrl()起作用。如果angular没有路由应用程序,你确定$location.url()会起作用吗?@SergioMarron根据文档(),它不依赖于路由。window.location
中的更改是活动的,$location
可以看到它们。我尝试了$location.path(),但它返回了一个空字符串。window.location.pathname起作用。@SergioMarron for me$location.url()也不起作用,但在查看调试器后,我发现有$location.absUrl()起作用。