使用AngularJs从url检索用户ID

使用AngularJs从url检索用户ID,angularjs,angular-routing,Angularjs,Angular Routing,我目前正在学习AngularJs,由于缺乏这方面的知识,我遇到了困难 我要做的是在URL中传入一个Id(guid),然后在我的控制器中检索它并将其传递给WebApi,以返回一些链接到该Id的数据 但是,我无法获得正确的路由,我当前的URL如下所示: 当我通过Id时,它会像这样 直截了当地说,我的页面名为index.html,在浏览网页、查看并基本复制了他/她提供的内容之后,我仍然无法从我的URL中检索该值 这是我当前的配置: var myApp = angular.module('chris

我目前正在学习AngularJs,由于缺乏这方面的知识,我遇到了困难

我要做的是在URL中传入一个Id(guid),然后在我的控制器中检索它并将其传递给WebApi,以返回一些链接到该Id的数据

但是,我无法获得正确的路由,我当前的URL如下所示:

当我通过Id时,它会像这样

直截了当地说,我的页面名为index.html,在浏览网页、查看并基本复制了他/她提供的内容之后,我仍然无法从我的URL中检索该值

这是我当前的配置:

var myApp = angular.module('christmasvip', ['ngRoute'])
   .config(['$routeProvider', function ($routeProvider) {
   $routeProvider.when('/:userId', { templateUrl: '/index.html', controller: 'mainController' }).
   otherwise({ redirectTo: '/index.html' });
}]);
这是我的控制器:

myApp.controller("mainController", function ($scope, $http, $routeParams) {

var userId = $routeParams.userId;

alert(userId);

});
然后操纵URL,使其看起来像:

但当我提醒用户ID时,它会说“罚款不足”

我的项目中还包括angular-route.js

任何帮助/解决方案都很好。

在中,您可以找到一个示例。。。请尝试使用URL:

你能试试这个网址吗


我假设您的根目录是您所说的配置中的
$routeProvider

.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/:userId', { templateUrl: '/index.html', controller:'mainController' }).
因此,您告诉它,“如果我们看到任何像这样的url'/anywhere',请接受'anywhere',并将其作为参数提供给
$routeParam
服务

然后你加上

otherwise({ redirectTo: '/index.html' });
这就告诉它,如果您得到任何其他url重定向到“/index.html”,但是
index.html
是这样的,所以它会希望将您路由到“/”,路由参数为“index.html”


另外,您需要确保index.html文件中有
,以便
$routeProvider
正确加载路由,这可能是您的主要问题,但我提到的第一件事可能是,一旦您添加它,就会出现错误。

感谢您回到我这里,我已经阅读了您链接的文档,我已经阅读了我使用了你提供的URL示例,但是我仍然无法获取Id,我确实使用了console.log($routeProvider);我可以在开发人员工具中看到该对象,但它是空的,但它没有提醒号码573637,相反,我仍然没有定义
otherwise({ redirectTo: '/index.html' });