gpt4 book ai didi

javascript - 使用 AngularJs 从 URL 获取值

转载 作者:行者123 更新时间:2023-12-02 14:05:11 25 4
gpt4 key购买 nike

我知道 Angularjs 有很多方法可以做到这一点。例如通过使用 $location 等。

但是如果 URL 如下所示,我无法找到如何获取值:

http://localhost:8000/test/1

如何从该 URL 获取值一?

对于路由,我使用 stateProvider:

export default angular.module("examino.userTestSession", ["ui.router"])
.config(function($stateProvider) {
$stateProvider
.state("testing", {
url: "/test",
controller: "UserTestSessionController",
templateUrl: "./app/components/userTestSession/user-test-session.html",
controllerAs: "vm"
})
.state("sessionExpired", {
url: "/sessionExpired",
controller: "UserTestSessionController",
templateUrl: "./app/components/userTestSession/user-test-session-expired.html",
controllerAs: "vm"
});
})

我可以在定义“测试”状态时获得该值吗?

最佳答案

只需将路由参数与 :param 一起使用

路线

export default angular.module("examino.userTestSession", ["ui.router"])
.config(function($stateProvider) {
$stateProvider
.state("testing", {
url: "/test/:id", //Add the parameter here
controller: "UserTestSessionController",
templateUrl: "./app/components/userTestSession/user-test-session.html",
controllerAs: "vm"
})
.state("sessionExpired", {
url: "/sessionExpired",
controller: "UserTestSessionController",
templateUrl: "./app/components/userTestSession/user-test-session-expired.html",
controllerAs: "vm"
});
})

Controller

var myId = $stateParams.id

注释

/test/:id 将匹配 /test/bob/test/1235/test/ > 但不是 /test/test/bob/other

/test/{id} 与上一个相同

/test/{id:int} 表示参数被解释为整数

/test/{id:[0-9]{1,8}} 表示 id 必须遵循提供的正则表达式

关于javascript - 使用 AngularJs 从 URL 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40106092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com