1

I think this is just basically a question of syntax, but I have this routing set up:

app.config(['$stateProvider', function ($stateProvider) {
    $stateProvider.state('addAlbum', {
        url: "/albums/create/:artistId",
        templateUrl: "public/albums/views/album.tpl.html",
        controller: "createAlbumController",
        data: {
            pageTitle: 'Create Album'
        },
        resolve: {
            albums: function (publicArtistServices) {
                return publicArtistServices.getAlbums(1);

            }
        }
    });
}]);

And I need that getAlbums() method to use the artistId passed in on the URL:

return publicArtistServices.getAlbums({need artistId from URL here});

I tried guessing at it by using return publicArtistServices.getAlbums($state.params[0]); but that just threw a "$state is not defined" error.

What's the right syntax here?

2 Answers 2

5

$stateParams is what you want to use for this.

app.config(['$stateProvider', function ($stateProvider) {
$stateProvider.state('addAlbum', {
    url: "/albums/create/:artistId",
    templateUrl: "public/albums/views/album.tpl.html",
    controller: "createAlbumController",
    data: {
        pageTitle: 'Create Album'
    },
    resolve: {
        albums: function ($stateParams, publicArtistServices) {
            return publicArtistServices.getAlbums($stateParams.artistId);
        }
    }
});

}]);

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

3 Comments

Oh, man, I didn't see this answer before I marked the other one correct. But yes, this is exactly the code that worked for me.
I feel like maybe I should mark this as the correct answer, even though the other answer is what lead me to solve the problem.
You should mark this correct since $stateParams is really what should be used here :)
1

You need to include the $state parameter (to "inject" it into your function):

        albums: function ($state, publicArtistServices) {
            return publicArtistServices.getAlbums($state.params[0]);
        }

If that doesn't work, use $route.current.params.artistId as shown here: https://stackoverflow.com/a/13433335/584846

2 Comments

Hm. Neither $state.params (after injection - can't believe I forgot to do that!) nor $route.current.params worked. However, per a comment on that link you referenced ("for people using ui-router and coming here: $stateParams is way to go"), I was able to get it to work using: function (publicArtistServices, $stateParams) { return publicArtistServices.getAlbums($stateParams.artistId); }
kudos to @hanothan for finding $stateParams too

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.