i want create app work : https://ionic-songhop.herokuapp.com
as can see, when click favorite button, item store in factory , can invoke in page (favorite page)
in case : use service store item data , create factory store pushed item.
here's code : (i store data in service)
.service('dataservice',function(){ var service=this; this.playerlist = [ { name: 'leonel messi', ava:"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68" }, { name: 'cristiano ronaldo', ava:"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68" }, { name: 'zlatan ibrahimovic', ava:"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68" }, { name: 'wayne rooney', ava:"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68" }, { name: 'michael carrick', ava:"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68" }, { name: 'phil jones', ava:"https://pbs.twimg.com/profile_images/473469725981155329/e24vfxa3_400x400.jpeg" }, { name: 'angel di maria', ava:"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68" } ]; }) .factory('user', function() { var play = { favorites: []} play.addtofavorites = function(song) { play.favorites.unshift(song); } play.removefromfavorites = function(player, index) { play.favorites.splice(index, 1); } return play; }) controller :
.controller('choosetabctrl', function($scope, dataservice, user) { $scope.dataservice=dataservice; $scope.addtofavorite = function (item) { user.favorites.unshift(dataservice.playerlist.indexof(), 1); } }) but when click favorite button on each item, list dont show in favorite page. possible in ionic app?
here's codepen : http://codepen.io/harked/pen/wvjqwp
there few issues code in codepen...
in controller referencing dataservice.playerlist.indexof() when player object playerlist (all lowercase). also, assume want indexof player line needs change to:
user.favorites.unshift(dataservice.playerlist.indexof(item)); // remove `, 1` otherwise you'll adding `1` array everytime and in view, need change following:
// wrong ng-click="addtofavorite(item)" // right ng-click="addtofavorite(player)" next, in listtabctrl change following:
$scope.players=dataservice; // $scope.players=dataservice.playerlist; then in view:
<ion-item ng-repeat="player in favorites" class="item item-avatar" href="#"> <img ng-src="{{players[player].ava}}"> <h2>{{players[player].name}}</h2> <p>back off, man. i'm scientist.</p> <ion-option-button class="button-assertive" ng-click="removeplayer(player, $index)"> <i class="ion-minus-circled"></i> </ion-option-button> </ion-item> i have posted working example of code on jsbin: http://jsbin.com/lukodukacu/edit?html,css,js,output
Comments
Post a Comment