【问题标题】:Get value from URL using AngularJs使用 AngularJs 从 URL 获取值
【发布时间】:2016-10-18 10:44:09
【问题描述】:

我知道在 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"
            });
    })

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

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    只需将路由参数与: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"
                });
        })
    

    控制器

    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 必须尊重提供的正则表达式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2017-03-01
      • 2017-01-08
      • 2023-01-25
      • 1970-01-01
      • 2013-06-02
      • 2013-05-10
      相关资源
      最近更新 更多