I have a list that show all top level objects, then you click on a parent object, and the view change to next state that is the child object that belongs to the parent.
I have a third state that i want to show the child object itself, not a list.
Demo is almost ready(i guess): http://plnkr.co/edit/6pqT0F?p=preview
When i click playlist1>playlist.singleCategory1 i want to show id: '1', title: 'Playlist1.singleCategory1'
Services:
angular.module('starter.services', [])
.service('PlaylistsService', function($q) {
return {
playlists: [
{ id: '1', title: 'Playlist1',
singleCategory: [
{ id: '1', title: 'Playlist1.singleCategory1' },
{ id: '2', title: 'Playlist1.singleCategory2' },
{ id: '3', title: 'Playlist1.singleCategory3' }
]
},
{ id: '2', title: 'Playlist2',
singleCategory: [
{ id: '1', title: 'Playlist2.singleCategory1' },
{ id: '2', title: 'Playlist2.singleCategory2' },
{ id: '3', title: 'Playlist2.singleCategory3' }
]
},
{ id: '3', title: 'Playlist3',
singleCategory: [
{ id: '1', title: 'Playlist3.singleCategory1' },
{ id: '2', title: 'Playlist3.singleCategory2' },
{ id: '3', title: 'Playlist3.singleCategory3' }
]
}
],
getPlaylists: function () {
return this.playlists
},
getPlaylist: function(playlistId) {
var dfd = $q.defer()
this.playlists.forEach(function(playlist) {
if (playlist.id === playlistId) dfd.resolve(playlist)
})
return dfd.promise
}
}
})
Controllers:
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope) {
})
.controller('PlaylistsCtrl', function($scope, playlists) {
$scope.playlists = playlists
})
.controller('PlaylistCtrl', function($scope, playlist) {
$scope.playlist = playlist
})
.controller('SingleCtrl', function($scope) {
});
app.js:
angular.module('starter', ['ionic', 'starter.services', 'starter.controllers'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "menu.html",
controller: 'AppCtrl'
})
.state('app.search', {
url: "/search",
views: {
'menuContent' :{
templateUrl: "search.html"
}
}
})
.state('app.browse', {
url: "/browse",
views: {
'menuContent' :{
templateUrl: "browse.html"
}
}
})
.state('app.playlists', {
url: "/playlists",
views: {
'menuContent': {
templateUrl: "playlists.html",
controller: 'PlaylistsCtrl',
resolve: {
playlists: function(PlaylistsService) {
return PlaylistsService.getPlaylists()
}
}
}
}
})
.state('app.playlist', {
url: "/playlists/:playlistId",
views: {
'menuContent': {
templateUrl: "playlists.playlist.html",
controller: 'PlaylistCtrl',
resolve: {
playlist: function($stateParams, PlaylistsService) {
return PlaylistsService.getPlaylist($stateParams.playlistId)
}
}
}
}
})
.state('app.single', {
url: "/playlists/:playlistId/:singleId",
views: {
'menuContent': {
templateUrl: "playlists.playlist.single.html",
controller: 'SingleCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/playlists');
});
In your SingleCtrl, you must get the playlistId from the stateParams
and again fetch that particular element from the service.
I've implemented it like below:
In your controller:
angular.module('starter.controllers', ["starter.services"])
.controller('SingleCtrl', function($scope, $stateParams, PlaylistsService) {
PlaylistsService.getSingle($stateParams.playlistId, $stateParams.singleId).then(function(data){
$scope.single = data;
})
});
In your service : I've created another method called getSingle
which will take both playlistid and singleId to fetch that particular object.
getSingle: function(playlistId, singleId) {
var dfd = $q.defer();
this.getPlaylist(playlistId).then(function(playlist) {
playlist.singleCategory.forEach(function(single) {
if(single.id==singleId) {
dfd.resolve(single);
}
});
});
return dfd.promise
}