私はangularjsを初めて使用し、チェックボックスをクリックするとモデル配列を作成したいと考えています。以下は私のコードです..
$scope.selectedAlbumSongs = [{
'name': 'song1',
'url': 'http://test/song1.mp3'
}, {
'name': 'song2',
'url': 'http://test/song2.mp3'
}, {
'name': 'song3',
'url': 'http://test/song3.mp3'
}];
$scope.playList = {};
HTML:
<fieldset data-role="controlgroup">
<legend>Select songs to play</legend>
<label ng-repeat="song in selectedAlbumSongs">
<input type="checkbox" name="{{song.url}}" id="{{song.name}}" ng-model="playList[song.url]">
<label for="{{song.name}}">{{song.name}}</label>
</label>
</fieldset>
チェックボックスをクリックすると、以下に示すようにプレイリストを更新する上記のコード
{
"http://test/test1.mp3": true,
"http://test/test2.mp32": true,
"http://test/test3.mp3": false
}
しかし、以下の形式で ng-model を作成し、チェックボックスがオフになっているときにオブジェクトを削除したい (たとえば、song3 のチェックを外すと、song3 オブジェクトが配列から削除されます)。このロジックの書き方を教えてください。
期待される:
[{
name: "song1",
url: "http://test/song1.mp3"
}, {
name: "song2",
url: "http://test/song2.mp3"
}]