我在Ionic框架中有一组显示电影列表的选项卡:
<script id="tabs.html" type="text/ng-template">
<ion-tabs tabs-style="tabs-icon-top" tabs-type="tabs-positive">
<ion-tab title="Movies" icon="ion-film-marker" href="#/tab/movies">
<ion-nav-view name="movies-tab"></ion-nav-view>
</ion-tab>
</ion-tabs>
</script>
<script id="movies.html" type="text/ng-template">
<ion-view title="'Movies'">
<ion-content has-header="true" padding="false">
<div class="list">
<a ng-repeat="item in movies" href="#/tab/movies/{{item.id}}" class="item item-thumbnail-left item-text-wrap">
<img ng-src="{{ item.image }}">
<h2>{{ item.title }}</h2>
<h4>{{ item.desc }}</h4>
</a>
</div>
</ion-content>
</ion-view>
</script>列表中的每个项目都链接到#/tab/movies/{{item.id}},例如#/tab/movies/27。我的电影是在控制器中定义的
.controller('MyCtrl', function($scope) {
$scope.movies = [
{ id: 1, title: '12 Rounds', desc: 'Detective Danny Fisher discovers his girlfriend has been kidnapped by a ex-con tied to Fisher\'s past, and he\'ll have to successfully complete 12 challenges in order to secure her safe release.', image: ''}
];我的页面路由如下:
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('tabs', {
url: "/tab",
abstract: true,
templateUrl: "tabs.html"
})
.state('tabs.movies', {
url: "/movies",
views: {
'movies-tab': {
templateUrl: "movies.html",
controller: 'MyCtrl'
}
}
})
$urlRouterProvider.otherwise("/tab/movies");
})现在我需要做的是,当点击上面列表中的每一项时,它会把它带到它自己的页面#/tab/movies/{{item.id}},在那里我可以显示像item.title或item.image这样的东西,以及一个后退按钮来转到列表。
为了做到这一点,据我所知,我需要创建一个带有信息占位符的空白ng-template,然后在单击时如何将此信息传递给它,但我不确定如何做到这一点。
发布于 2014-04-16 23:15:17
在您的stateProvider配置中,您需要为电影id添加一个placeholderFor,如下所示:
.state('tabs.movies', {
url: "/movies/:id",
views: {
'movies-tab': {
templateUrl: "movies.html",
controller: 'MyCtrl'
}
}
})请参阅https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service
发布于 2014-03-03 19:31:35
您正在寻找的可能是服务/工厂,您可以在其中存储电影列表,然后检索MyCtrl的完整列表,或者仅检索电影页面的单个电影对象。
angular.module('myAppName')
.factory('MovieService', function () {
return {
MoviesList: [
{movie object}
],
GetAllMovies: function () {
return this.MoviesList;
},
GetMovieById: function (id) {
//iterate MoviesList and return proper movie
}
}
}然后可以将该服务注入到控制器中
.controller('MyCtrl', function($scope, MoviesService) {
$scope.movies = MoviesService.GetAllMovies();
}电影视图控制器也是如此:
.controller('ShowMyMovie', function($scope, MoviesService) {
$scope.movie = MoviesService.GetMovieById(//retrieve_id_from_routing_service);
}然后,在此视图的模板中,您可以简单地使用{{movie.title}}来显示信息
https://stackoverflow.com/questions/22141720
复制相似问题