尝试在AngularJs

时间:2016-10-21 08:17:13

标签: angular

我正在尝试从以下链接获取网址参数:localhost:8000\testSession\1&otp="91PVSR" 网址格式为:localhost:8000\testSession\id&otp="otpvalue"

我在我的控制器中使用stateProvider,如下所示:

class UserTestSessionController {
    constructor($http, $state, UserTestSessionService, $stateParams) {
        this.$http = $http;
        this.$state = $state;
        this.$stateParams = $stateParams;
        this.userTestSessionService = UserTestSessionService;
    }

    startSession() {
        let self = this;
        let sessionId = this.$stateParams.id;
        let otp = this.$stateParams.otp;

        this.userTestSessionService.startSession(sessionId, otp).then(function(data) {
            if(data.isError) {
                self.$state.go("sessionExpired");
            }
            else {
                self.$state.go("testingSession");
            }
        });
    }
}

export default UserTestSessionController;

这就是用状态定义URL的方式:

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

当我尝试获取ID和OTP值时会出现问题:

Id=1&otp="someValue"otpundefined

enter image description here

在控制器中读取此值的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我认为您的路线声明中有错误。试试这个(更改网址):

$stateProvider
        .state("testingSession", {
            url: "/testSession?:id:otp",
            controller: "UserTestSessionController",
            templateUrl: "./app/components/userTestSession/user-test-session.html",
            controllerAs: "vm"
        })

使用$ stateParams.id和$ stateParams.otp

访问