0

I know there is a many methods to do this in Angularjs. For example by using $location etc.

But I am unable to find how to get value if URL looks like this:

http://localhost:8000/test/1

How to get value one from that URL?

For routing I am using 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"
            });
    })

Can I maybe get that value when defining "testing" state?

1 Answer 1

2

Simply use the route parameters with :param

Routes

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

Notes

/test/:id will match /test/bob, /test/1235 and /test/ but not /test or /test/bob/other

/test/{id} is the same as the previous one

/test/{id:int} means that the param is interpreted as an integer

/test/{id:[0-9]{1,8}} means that the id must respect the regex provided

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.