2014-09-10 82 views
0


目前,主列表HTML工作點擊列表項,頁面轉變,但詳細信息頁面只顯示沒有項目的詳細信息,除了()

<div class="post row" ng-repeat="(postId, post) in posts"> 
    <a href="{{ post.url }}">{{ post.title }}</a> 

但是當我點擊的項目(的一個許多在列表中)並轉到另一頁,新頁面不會詳細顯示該項目?


當添加下面的行中,包括$ stateParams在依賴關係,到控制器JS文件,{{post.title}}顯示,但沒有將數據通過。

$scope.post = $scope.posts[$stateParams.id] 



UPDATE
這是國家代碼。 (忽略缺少的語法......我縮短它)。有人幫助解決了上一個問題,併爲查看部分提供了以下代碼(最後2個州)。

.state('tab.view', { 
     url: '/posts/:postId', 
     views: { 
     'tab-view': { 
      templateUrl: 'templates/tab-showpost.html', 
      controller: 'PostViewCtrl' 



如何細節點擊列表項後訪問。

app.controller('PostViewCtrl', function ($scope, $stateParams, Post) { 
    $scope.Post = Post.find($stateParams.postId); //i think this may be broken 

回答

1

在你的問題中缺少的關鍵之一是你的stateProvider是如何配置的。請確保您的狀態已正確設置url以通過狀態參數發送數據。我有a codepen here,它顯示了一種方法來獲得一個項目列表,點擊一個項目將把用戶的細節。需要注意的狀態如何設置?

angular.module('ionicApp', ['ionic']) 

.config(function($stateProvider, $urlRouterProvider) { 

    $stateProvider 
    .state('tabs', { 
     url: "/tab", 
     abstract: true, 
     templateUrl: "tabs.html" 
    }) 
    .state('tabs.home', { 
     url: "/home", 
     views: { 
     'home-tab': { 
      templateUrl: "home.html", 
      controller: 'HomeTabCtrl' 
     } 
     } 
    }) 
    .state('tabs.detailview', { 
     url: "/detailview/:index", //NOTE: :index is how you use $stateParams later on. 
     views: { 
     'home-tab': { 
      templateUrl: "detailview.html", 
      controller: 'DetailviewTabCtrl' 
     } 
     } 
    }); 


    $urlRouterProvider.otherwise("/tab/home"); 

}); 

然後,在你的控制器......

.controller('DetailviewTabCtrl', function($scope,$stateParams) { 
    $scope.id = parseInt($stateParams.index);//NOTE: This is the same 'index' 
    $scope.previous = parseInt($stateParams.index) - 1; 
    $scope.next = parseInt($stateParams.index) + 1; 
}); 
+0

嗯我過渡到另一個頁面的作品只是內容不是基於ID出現......或者在你的情況下索引:。我更新了狀態! :) – Thinkerer 2014-09-11 14:49:37

1

我不知道是什麼{{post.url}}是在ng-repeat;但我認爲你需要一個不同的$state來處理細節視圖。

做這樣的事情:

<div class="post row" ng-repeat="(postId, post) in posts"> 
    <a ui-sref="postDetail({id:post.id })">{{ post.title }}</a> 
</div> 

然後,您需要在您的應用程序config$stateProvider的狀態定義是這樣的:

.state('postDetail', { 
     url: "/post/:id", 
     //NOTE: :id will be accessed from the controller using $stateParams later on. 
     templateUrl: "post_detail.html", 
     controller: 'PostDetailCtrl' 
    }) ... 

應該這樣做。

+0

感謝彼得,爲什麼是id:post.id而不是postId:post.id?此外,我可以去新的頁面...只是有些事情與ID錯誤...內容沒有顯示在新的頁面上。 – Thinkerer 2014-09-11 16:09:41

+0

也新網頁網址不顯示:ID?...它像...標籤/帖子,而不是標籤/帖子/:ID – Thinkerer 2014-09-11 16:17:27

+0

在狀態配置,你想要的任何參數是你會傳遞ui-sref作爲一個對象。你也可以做'{{ post.title }}'。我在想'postId'是一個重複的別名。在迭代過程中訪問'post'對象的'id'看起來更合適。 – Peter 2014-09-11 16:22:36

相關問題