0

My HTML is as follows:

<a ui-sref="videoParent.Display.video({videoName:'[[sVid.slug]]', videoId:'[[sVid.videoID]]'})"><p>[[sVid.name]]</p></a>

I then retrieve the parameters videoName and videoId in my js file as follows

.state("videoParent.Display.video", { 
            url: "/video/:videoName/:videoId", 
            views: {
                'video_info@videoParent': { 
                    templateUrl: "/partials/starred_video.html", 
                    controller: "starredVideoController",
                    controllerAs: "starredVidsCtrl",
                    resolve: {
                        retrivedVideo: ['videosService', '$stateParams', function(videosService, $stateParams) {
                            if ($stateParams.videoId) {                                   
                                var video = videosService.getVideoById($stateParams.videoId);
                                return video;
                            }
                        }]
                    }
                },

In my url I get a result like this:

../video/data-statistician/5

However, I do not wish to show the videoId (5) and would rather have my output as:

../video/data-statistician

I can achieve the desired result by writing the url part of my state as:

url: "/video/:videoName

but the problem is that if I write my code as above, the videoId will not be included in the $stateparams object.

How can I pass the video ID to the state without showing in the URL?

1
  • @lin could you kindly show me what you mean with a code illustration? Thank you for your response. Commented Feb 3, 2017 at 10:21

2 Answers 2

2

If you dont wish to show ID in URL but need to pass ID using stateparams, try this

.state("videoParent.Display.video", { 
            url: "/video/:videoName",
           params:{videoId:null}, 
            views: {
            'video_info@videoParent': { 
                templateUrl: "/partials/starred_video.html", 
                controller: "starredVideoController",
                controllerAs: "starredVidsCtrl",
                resolve: {
                    retrivedVideo: ['videosService', '$stateParams', function(videosService, $stateParams) {
                        if ($stateParams.videoId) {                                   
                            var video = videosService.getVideoById($stateParams.videoId);
                            return video;
                        }
                    }]
                }
                }

NOTE: Declaration of params in a state definition has changed to params: { id: {} } from params: ['id']

<a ui-sref="videoParent.Display.video({videoName:{sVid.slug}, videoId:{sVid.videoID}})"><p>[[sVid.name]]</p></a>

UI-router version: ~v.0.2.10

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

1 Comment

Worked like a charm. Thank you!
0

As you suggested, you can pass parameters without having them show in the URL by setting the url to

url: "/video/:videoName

You can add additional parameters (not in the URL) by specifying:

    params: {
        videoId: null
    }

If you want to navigate to the state and pass the parameters, you can do either:

In HTML:

<a ui-sref="videoParent.Display.video({videoName: 'Rambo', videoId: 5})">Go to video</a>

In Javascript:

$state.go('videoParent.Display.video', {videoName: 'Rambo, videoId: 5});

4 Comments

That is what I am doing, Im using the HTML solution. Read the question carefully again and see were my problem is. Thank you for responding though.
@Tatenda its mug to downvote the guys who try to help ya. This is the same approach that you marked as right answer (above).
@lin my reputation is 24 and less than the 125 needed for me to be able to downvote. It was not me who downvoted
Yes I forgot this important part when I first wrote it @Tatenda

Your Answer

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