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 回答