Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I want to pass the attribute film.title to the state template without having to specify it in the state URL.

index.html:

<a ui-sref="showtimes({ filmId: film.uid, filmTitle: film.title })">

app.js:

    .state('showtimes', {
        url: '/showtimes/:filmId',
        controller: function($scope, $http, $stateParams) {
            $http.get('api/showtimes/?film_id=' + $stateParams.filmId).success(function(data) {
                $scope.showtimes = data;
            });
        },
        templateUrl: 'static/showtimes.html'
    });

showtimes.html:

<p>{{film}}</p>

I tried adding $scope.film = $stateParams.filmTitle; to the controller. It didn't work. I also tried $scope.film = $state.params.filmTitle; without any more luck.

share|improve this question
    
looks like filmTitle is in response data from $http request, I don't think you can get that from $stateParams. –  YOU Apr 11 at 15:21

1 Answer 1

up vote 1 down vote accepted

You can specify non-URL parameters with params property:

.state('showtimes', {
  url: '/showtimes/:filmId',
  params: {
    filmTitle: undefined // or "default Title"
  },
  controller: function($scope, $http, $stateParams) {
    console.log($stateParams.filmTitle);
    // ...
  },
  // ...
});
share|improve this answer
    
This works. However, after adding params: { filmTitle: undefined },, I cannot reload (or copy/paste) the state URL and keep the state: it loads the default URL instead. –  Arnaud Renaud Apr 11 at 15:47
    
@ArnaudRenaud, what do you mean by "keep the state"? what is "default URL"? –  New Dev Apr 11 at 15:49
    
the default URL is the one I have configured like this: $urlRouterProvider.otherwise('/films');. Keeping the state is being able to refresh the page without losing the page layout. –  Arnaud Renaud Apr 11 at 15:52
    
Actually, after adding filmTitle to params, the proper state URL remains after reloading but the layout of the page is back to default. –  Arnaud Renaud Apr 11 at 15:55
    
When you refresh the page, it loses filmTitle obviously - the only seed parameters are those that are defined on the URL (not what you intended, though, for filmTitle), so the view would render as if filmTitle === undefined. As to default url - then, you must not be matching /showtimes/:filmId –  New Dev Apr 11 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.