1
我正在研究Ionic應用程序,其中,我需要將項目(json對象)傳遞給視圖並將項目綁定到那裏。Angular:將對象傳遞給同一控制器中的不同視圖
我已經經歷了很多類似的問題了,有可能:
Unable to access $scope variable within same controller
AngularJS: Pass an object into a state using ui-router
但是,似乎沒有一個是我的情況下工作。
這裏是index.html
頁的我的HTML標記(從我傳遞item
這裏):
<div class="list card slide-in" ng-repeat="item in posts track by $index">
<h2 ng-bind="item.title" class="assertive"></h2>
//comes inside ng-repeat loop; I'm passing the whole json object as parameter to next view
<button class="button button-full" ng-click="getPostDetail(item);"> Read More </button>
</div>
的Html頁面posts.html
的標記(在這裏我需要訪問item
):
<ion-view view-title="Read More ">
<ion-content class="card-background-page ">
<div class="list padding-20">
<h2 ng-bind="selectedPost.title" class="assertive"></h2>
</div>
</ion-content>
</ion-view>
我的配置:
app.config(function ($stateProvider, $urlRouterProvider, localStorageServiceProvider) {
$stateProvider
.state('tabs.posts', {
url: "/posts",
params: {
obj: null // as per other suggestion, added a param here
},
views: {
'home-tab': {
templateUrl: "templates/post.html",
controller: 'main'
}
}
})
我的控制器:
app.controller('main', function ($scope, $http, $state, $ionicModal, $location, $stateParams, localStorageService) {
$scope.getPostDetail = function (data) {
var selectedPost = data;
$state.go("tabs.posts", { obj: selectedPost });
}
})
在點擊按鈕時,帖子查看加載成功,但沒有內容被綁定,即認爲是空白
我已經嘗試將它傳遞給'$ scope.selectedPost'(儘管這兩個視圖共享相同的控制器)給出了相同的輸出。 – Cyberpks