简体   繁体   中英

Accessing the 'id' value from the side menu example from the Iconic framework using AngularJS and

With the Iconic Framework side menu example how do I access the 'id' and 'Title' value from the controller?

.controller('PlaylistsCtrl', function($scope) {$scope.playlists = [
{ title: 'Traffic', id: 1 },
{ title: 'Litter', id: 2 },
{ title: 'Marine', id: 3 }]

In the Playlist.html template file?

<ion-view view-title="Playlist One">
 <ion-content>
  <h1>Playlist</h1>
 </ion-content>
</ion-view>

The other relevant code from app.js file is:

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

And the controller - I think:

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

You should bind PlaylistsCtrl to this ion-view from its state mentioned in config phase.

<ion-view view-title="Playlist One">
 <ion-content>
  <h1>Playlist</h1>
  <div ng-repeat="playlist in playlists">
  Number: {{playlist.id}} | Title: {{playlist.title}}
  </div>
 </ion-content>
</ion-view>

Its Simple. you just have to insure that in your Config you have assigned controller to the view.

<ion-view view-title="Playlist One">
 <ion-content>
 <h1>Playlist</h1>

<ul>
  <li ng-repeat="p in playlists">
     ID: {{p.id}}
  </li>
</ul>
 </ion-content>
</ion-view>

Updated

<ion-view view-title="Playlist One">
 <ion-content>
 <h1>Playlist</h1>
  <!-- Pass playlist as a parameter named **p** here onclick function-->
<ul>
  <li ng-repeat="p in playlists" ng-click="selected(p)">
     ID: {{p.id}}
  </li>
</ul>
 </ion-content>
</ion-view>

Your controller will look like this.Only you need to add selected function in your controller like this.

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

   $scope.selected=function(playlist){
     $scope.selectedPlaylist=playlist;
     console.log($scope.selectedPlaylist.id)
   }
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM