I'm creating an app using Spotify API. This app can: 1. find albums by artist name through an input field 2. once the albums are shown, the user can click the album's title and see its tracks
I have created 2 views: 1. the albums view 2. the tracks view, child of albums view.
I'd like to show the tracks ui-view of a specific clicked album - with its ID - into the albums' ng-repeat, but the result is that I have the same tracks list of the clicked album throughout all albums list. How can I show the album's track ui-view into the clicked album container that is a result of an ng-repeat directive?
Here's the code.
Routing:
------
.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('albums', {
url: '/',
templateUrl: 'views/main.html',
controller: 'MainCtrl'
}).
state('albums.tracks', {
url: ':id/tracks',
templateUrl: 'views/tracks.html',
controller: 'TracksCtrl'
});
})
-----
The albums view:
<div ng-if="albums.items">
<h1>{{searchedArtist}}'s Album List:</h1>
<!-- albums ng-repeat -->
<div ng-repeat="album in albums.items | unique:'name'" id="{{album.id}}">
<img ng-src="{{album.images[1].url}}" width="100">
<!-- the action to translate the state to tracks view -->
<a ui-sref="albums.tracks({id : album.id})">{{album.name}}</a>
<!-- tracks view -->
<div ui-view></div>
</div>
Main albums controller:
angular.module('spotifyAngularApp')
.controller('MainCtrl', function ($scope, spotifyService) {
var options = {
'limit': 10
};
$scope.searchAlbums= function () {
var sanitizeArtist = $scope.artist.split(' ').join('+');
$scope.searchedArtist = $scope.artist;
spotifyService.search(sanitizeArtist, 'album', options).then(function(data) {
$scope.albums = data.albums;
});
};
});
The tracks controller:
angular.module('spotifyAngularApp')
.controller('TracksCtrl', function ($scope, $stateParams, spotifyService) {
console.log($stateParams);
spotifyService.albumTracks($stateParams.id).then(function(data){
$scope.tracks = data.items;
});
});