我目前正在学习AngularJs,由于我对此缺乏了解,我已经碰到了一堵砖墙。
我尝试做的是在URL中传入Id(guid),在我的控制器中我将检索它并将其传递给WebApi以返回链接到该Id的一些数据。< / p>
但是我无法正确显示路线,我目前的网址如下:
当我传入Id时,它看起来像这样
非常简单,我的网页名为index.html,在浏览网页并查看this question并基本上复制了他/她提供的内容后,我仍然无法从我的网址中检索该值。
这是我目前的配置:
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,看起来像:
但是,当我提醒用户时,它会说'#34; underfined&#34;
我的项目中还包括angular-route.js
任何帮助/解决方案都会很棒。
答案 0 :(得分:0)
在documentation of ngRoute中,您可以找到一个示例...尝试使用网址:http://localhost:15216/index.html#/573637
// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
答案 1 :(得分:0)
答案 2 :(得分:0)
您在说出的配置中混淆了$routeProvider
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/:userId', { templateUrl: '/index.html', controller:'mainController' }).
所以你告诉它,“如果我们看到任何这样的网址'/任何'采取'任何'并将其作为参数提供给$routeParam
服务
然后你添加
otherwise({ redirectTo: '/index.html' });
告诉它,如果你将任何其他网址重定向到“/index.html”,但是index.html
是某种东西,所以它会想要用路由参数作为'索引将你路由到'/'。 html'
你还需要确保你的index.html文件中有<ng-view></ng-view>
$routeProvider
才能正确加载路线,这可能是你的主要问题,但我提到的第一件事就是添加后可能会出错。