0

新しいアイテムを配列にプッシュすると、bpm に関係なく最後に追加されます。ページを更新すると、すべてが正しくソートされます。

//view
<form ng-submit="add(newSong)">
  <input ng-model="newSong.artist" placeholder="Artist..">
  <input ng-model="newSong.title" placeholder="Title..">
  <input ng-model="newSong.bpm" placeholder="BPM..">
  <input ng-model="newSong.key" placeholder="Key..">
  <input ng-model="newSong.year" placeholder="Year..">
  <button ng-show=''></button>
</form>

<div ng-repeat="song in songs | orderBy:'bpm'">
  {{song.bpm}} / {{song.artist}} - {{song.title}}
</div>

//controller
$http.get('/api/songs').success(function(data) {
    $scope.songs = data
})

$scope.add = function(newSong) {

    var song = {
        artist: newSong.artist
      , title: newSong.title
      , bpm: newSong.bpm
      , key: newSong.key
      , year: newSong.year
    }

    $scope.songs.push(song)

    $http.post('/api/songs', song).success(function(data) {
        console.log(data)
    })

}

これを plunker で書いたところ、正常に動作しましたが、自分の環境の何が問題なのかわかりません (v1.2.25)

4

1 に答える 1

1

私のモデルは、BPM に数値を使用します。これで問題が解決しました:

var song = {
        artist: newSong.artist
      , title: newSong.title
      , bpm: parseInt(newSong.bpm)
      , key: newSong.key
      , year: newSong.year
    }
于 2014-10-01T23:02:23.543 に答える