简体   繁体   English

AngularJS UI路由器:将变量添加到状态范围而不在URL中指定

[英]AngularJS UI Router: add variable to scope of state without specifying it in URL

I want to pass the attribute film.title to the state template without having to specify it in the state URL . 我想将属性film.title传递给状态模板, 而不必在状态URL中指定它

index.html: index.html的:

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

app.js: 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: showtimes.html:

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

I tried adding $scope.film = $stateParams.filmTitle; 我尝试添加$scope.film = $stateParams.filmTitle; to the controller. 到控制器。 It didn't work. 没用 I also tried $scope.film = $state.params.filmTitle; 我还尝试了$scope.film = $state.params.filmTitle; without any more luck. 再也没有运气了。

You can specify non-URL parameters with params property: 您可以使用params属性指定非URL参数:

.state('showtimes', {
  url: '/showtimes/:filmId',
  params: {
    filmTitle: undefined // or "default Title"
  },
  controller: function($scope, $http, $stateParams) {
    console.log($stateParams.filmTitle);
    // ...
  },
  // ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM