【发布时间】:2013-08-21 00:06:22
【问题描述】:
我有一个视图,其中包含 ng-repeat 中的帖子列表。当用户单击帖子时,他们会被路由到所选帖子的详细信息页面。
我已经设置了一些路由,但是在弄清楚如何处理我注入控制器以使详细视图加载正确的帖子的 id 参数时遇到了问题。
我也不确定是否传递了正确的值作为帖子的标识符。这些帖子作为数组存储在 Firebase 中,我不确定在读入数据以存储 FIrebase id 时是否需要做一些事情。我加载的数据中没有明确的唯一标识符。
帖子列表视图:
<div id='posts' ng-controller="PostsCtrl">
<ul>
<div id='post' ng-repeat="post in posts track by $id($index)">
<div id='postMedia'>
<img id='media' ng-click="go('/post/{{$id}}')" ng-src= {{post.attachment}} />
</div>
<div id="articleRight">
<div ng-click="go('/post')">{{post.message}}</div>
后控制器:
myApp.controller('PostsCtrl', ['$scope', 'angularFire', '$routeParams',
function ($scope, angularFire, $routeParams) {
var url = 'https://inviter-dev.firebaseio.com/posts';
$scope.items = angularFire(url, $scope, 'posts', [] );
$scope.currentPost = $scope.items[$routeParams.postid]
帖子详情视图:
<div class="container">
<h1>{{$scope.$currentPost.message}}</h1>
<ul>
<div id='postMedia'>
<img ng-click="" ng-src={{post.attachment}} />
</div>
<div ng-click="">
<div ng-click="">{{posts[$route.current.params.postid].message}}</div>
路由:
$routeProvider.when('/post/:postid', {
templateUrl:'partials/post.html',
authRequired: true,
controller:'PostsCtrl'
})
【问题讨论】:
-
您在这里遇到的问题是什么?
-
不知道如何让子视图绑定到选中的帖子。
-
这里有一个调试指针,可以帮助您入门。检查您的 $scope.items 对象以确定它的索引方式(按 id?按数字?)。然后检查您尝试用作索引的变量(在您的情况下为 $id)。您应该会发现它们不匹配($id 将类似于 001、002 等,而 $scope.items 可能索引为 0、1 等)。
-
谢谢。我切换回字典,因为我认为我引用的 AngularFire 补丁只适用于字典。我确实检查了这些物品以弄清楚这一点。
标签: angularjs firebase angularjs-ng-repeat angularfire