Dimas Adi Andrea
Dimas Adi Andrea

Reputation: 453

How to get parameters from view to controller in Ionic

here is my view

<ion-view view-title="Playlists">
  <ion-content>
    <ion-list>
      <ion-item ng-repeat="playlist in playlists" href="#/app/playlists/{{playlist.id}}">
        {{playlist.title}}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

Here is my state templating

  .state('app.single', {
    url: '/playlists/:playlistId',
    views: {
      'menuContent': {
        templateUrl: 'templates/playlist.html',
        controller: 'PlaylistCtrl'
      }
    }
  });

and here is my controller

.controller('PlaylistCtrl', function($scope, $stateParams) {

});

how to get the parameters from the view in the controller?

Upvotes: 0

Views: 1288

Answers (3)

Sajeetharan
Sajeetharan

Reputation: 222582

You can directly access the variable from

stateParams

 .controller('PlaylistCtrl', function($scope, $stateParams) {
  $scope.id= $stateParams.playlistId;  
}
]);

Upvotes: 1

Phil
Phil

Reputation: 164766

Any state URL parameters are available by name in $stateParams, eg

$stateParams.playlistId

Also, you should be using ui-sref instead of href in your HTML links

ui-sref="app.single({playlistId: playlist.id})"

Upvotes: 1

Santosh Shinde
Santosh Shinde

Reputation: 6063

Use the following :

 .controller('PlaylistCtrl', function($scope, $stateParams) {

    //In playlistId your input param 
      var playlistId = $stateParams.playlistId;

    //more...
 });

Hope this will help you !!

Upvotes: 2

Related Questions