javascript - AngularJS UI 路由器 : add variable to scope of state without specifying it in URL

标签 javascript angularjs angular-ui-router

我想传递属性film.title状态模板无需在状态 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'
    });

showtime.html:

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

我尝试添加$scope.film = $stateParams.filmTitle;到 Controller 。它不起作用。我也尝试过$scope.film = $state.params.filmTitle;没有更多的运气。

最佳答案

您可以使用 params 属性指定非 URL 参数:

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

关于javascript - AngularJS UI 路由器 : add variable to scope of state without specifying it in URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29579430/

相关文章:

javascript - 是否可以在 Angular Controller 方法中对整个对象进行建模绑定(bind)?

javascript - 将对象参数传递给 setInterval

angularjs - angular.service

angularjs - 使用不同状态的通用模板

javascript - 如何更改jquery函数变量?

javascript - 在字符串javascript中替换使用正则表达式

javascript - AngularJS 测试 Controller 与指令中的谷歌地图

javascript - HammerJS (v2) 滑动和平移禁用在 iOS 中触发的链接

javascript - 为 Angular Material $mdDialog 提供 Angular ui-router 状态的 url

javascript - 如何防止兄弟状态 Controller 刷新?