2016-10-21 124 views
0

我試圖從鏈接看起來像這樣得到網址參數:localhost:8000\testSession\1&otp="91PVSR" 其中URL格式是這樣的: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" 
      }) 
}) 

的問題發生磨片n我嘗試獲得ID和OTP值:

Id=1&otp="someValue"otpundefined

enter image description here

什麼是閱讀器這個值的正確方法?

+0

你可以嘗試沒有在國家的URL規範的'otp'了嗎? – SylvainB

+0

@SylvainB我剛剛做了,值是否一樣? otp值未定義... –

+0

@ nemo_87您是否試圖更準確地指定您的網址模板?像這樣:url:「/ testSession/{id:[0-9] +}?otp」 – Nedev

回答

0

我認爲你的路線聲明中有錯誤。試試這個(以URL變化):

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

,並獲得與$ stateParams.id和$ stateParams.otp