Angular orderBy不对数组推送进行重新排序

时间:2014-10-01 22:59:00

标签: angularjs angularjs-orderby

当我将一个新项目推送到数组时,无论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)

1 个答案:

答案 0 :(得分:1)

我的模型使用数字表示BPM。这解决了我的问题:

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