我正在尝试从思想家实现这个教程。IO: https://thinkster.io/ionic-framework-tutorial/
我已经在步骤3:"构建接口功能";在"添加,删除和检索收藏的歌曲"方面遇到了一点问题。
我遵循了以下步骤:"在services.js"中创建一个User工厂直到sendFeedback()方法的开始行"将当前歌曲添加到我们的收藏夹中"。
当我尝试点击收藏按钮,并去收藏的页面,没有什么发生。当前歌曲已添加到收藏列表。
这是我在controller.js中的代码.controller('DiscoverCtrl', function($scope, $timeout, User) {
$scope.songs = [
{
"title":"Stealing Cinderella",
"artist":"Chuck Wicks",
"image_small":"https://i.scdn.co/image/d1f58701179fe768cff26a77a46c56f291343d68",
"image_large":"https://i.scdn.co/image/9ce5ea93acd3048312978d1eb5f6d297ff93375d"
},
{
"title":"Venom - Original Mix",
"artist":"Ziggy",
"image_small":"https://i.scdn.co/image/1a4ba26961c4606c316e10d5d3d20b736e3e7d27",
"image_large":"https://i.scdn.co/image/91a396948e8fc2cf170c781c93dd08b866812f3a"
},
{
"title":"Do It",
"artist":"Rootkit",
"image_small":"https://i.scdn.co/image/398df9a33a6019c0e95e3be05fbaf19be0e91138",
"image_large":"https://i.scdn.co/image/4e47ee3f6214fabbbed2092a21e62ee2a830058a"
}
];
// initialize the current song
$scope.currentSong = angular.copy($scope.songs[0]);
$scope.sendFeedback = function (bool) {
// first, add to favorites if they favorited
if (bool) User.addSongToFavorites($scope.currentSong);
// set variable for the correct animation sequence
$scope.currentSong.rated = bool;
$scope.currentSong.hide = true;
$timeout(function() {
// $timeout to allow animation to complete before changing to next song
// set the current song to one of our three songs
var randomSong = Math.round(Math.random() * ($scope.songs.length - 1));
// update current song in scope
$scope.currentSong = angular.copy($scope.songs[randomSong]);
}, 250);
}
})
.controller('FavoritesCtrl', function($scope, User) {
// get the list of our favorites from the user service
$scope.favorites = User.favorites;
})
service.js
中的代码angular.module('songhop.services', [])
.factory('User', function() {
var o = {
favorites: []
}
return o;
//Method for adding songs to the favorites array:
o.addSongToFavorites = function(song) {
// make sure there's a song to add
if (!song) {
return false; }
// add to favorites array
o.favorites.unshift(song);
}
});
有人知道吗?非常感谢。
您的代码有一个小问题,return
语句在服务声明完成之前出现。将return
移到最后