1

I am working on an Ionic app where, I need to pass an item (json object) to a view and bind the items there.

I've gone through a lot of similar questions, likely:

Unable to access $scope variable within same controller

AngularJS: Pass an object into a state using ui-router

But, seems like none is working in my case.

Here is my HTML markup of index.html page (from where I'm passing 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 Markup of posts.html page (where I need to access 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>

My Config:

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'
      }
  }
})

My Controller:

app.controller('main', function ($scope, $http, $state, $ionicModal, $location, $stateParams, localStorageService) {
    $scope.getPostDetail = function (data) {
        var selectedPost = data;
        $state.go("tabs.posts", { obj:  selectedPost });
    }

})

On click of button, the Posts View loads successfully, but no content is bound i.e. the view is blank

Community
  • 1
  • 1
Cyberpks
  • 1,401
  • 6
  • 21
  • 51

1 Answers1

0

Unless I'm missing something, selectedPost needs to be on $scope. Otherwise it cannot be accessed from the view. So something like this.

    $scope.getPostDetail = function (data) {
        $scope.selectedPost = data;
        $state.go("tabs.posts", { obj:  selectedPost });
    }
deadwards
  • 2,109
  • 1
  • 16
  • 27
  • I've already tried passing it to `$scope.selectedPost` (even though, both views share same controller), gives the same output. – Cyberpks Aug 02 '16 at 13:39