简体   繁体   English

如何在angularjs,ui-router和$ stateParams中使用url参数

[英]How to use url parameters in angularjs, ui-router and $stateParams

I have followed the instructions from https://github.com/angular-ui/ui-router/wiki/URL-Routing but cannot get this to work properly. 我已经按照https://github.com/angular-ui/ui-router/wiki/URL-Routing中的说明进行操作,但是无法使其正常工作。

I have the following: 我有以下内容:

var application = angular.module('application', ['ui.router']);

application.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
$urlRouterProvider.otherwise("/test");

$stateProvider
    .state('index', {
        url: "/test/:param",
        templateUrl: "App/Test.html", 
        controller: function ($scope, $stateParams) {
            alert($stateParams.param);
        }
    });   

$locationProvider.html5Mode(true);
});

Without the /:param this works as you would expect - ie the ui-view is correctly populated with Test.html. 没有/:param,它将按您期望的那样工作-即ui-view正确地填充了Test.html。 However, whenever I put the /:param in I get an error. 但是,每当我将/:param放入时,都会出现错误。 The error is: 错误是:

GET http://localhost:3880/test/App/Test.html 404 (Not Found)

App is the route of my angular stuff and Test.html should have a path of 应用程序是我的棱角分明的东西的路线,并且Test.html应该具有以下路径

http://localhost:3880/App/Test.html 

which it does if not trying /:param. 如果不尝试/:param,它会这样做。 However, when trying /:param you can see that there is an extra /test/ in the path before /App. 但是,在尝试/:param时,您会看到/ App之前的路径中还有一个额外的/ test /。

Please someone help, as I would like to consume the parameter in the controller once it is correct. 请有人帮忙,因为我想在控制器正确后使用该参数。

您用于此路由的URL应该是这样的: http://localhost:3880/test/app其中app是param。

Use absolute path for templateUrl. 为templateUrl使用绝对路径。 relative url wont work. 相对网址不会起作用。

templateUrl: "/path/to/App/Test.html",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM