如何在AngularJS / Ionic中将数据推送到工厂/服务:(来自Thinkster.io的案例研究离子框架教程)

时间:2015-07-09 03:31:57

标签: angularjs ionic-framework

我正在尝试从Thinkers.io实施本教程:https://thinkster.io/ionic-framework-tutorial/

我已经在第3步:“Building Interface Functionality”并且在“添加,删除和检索收藏的歌曲”方面遇到了一些问题。

我按照以下步骤操作:“在services.js中创建一个用户工厂”,直到“在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);
}

});

有人有线索吗? 非常感谢。

1 个答案:

答案 0 :(得分:1)

您的代码存在一个小问题,def col_to_index(col): A = ord('A') return sum(i * 26 + (ord(c) - A) for i, c in enumerate(col[::-1].upper())) 语句在服务声明完成之前出现。将return移到最后一个。