将 ng-repeat table 列值传递给数组
Pass ng-repeat table column values to an array
在表格中搜索艺术家或歌曲时,会返回 table 首歌曲,其中的列标题是与歌曲相关的信息描述。我的目标是仅将 "Track Id" 列值发送到数组以供以后使用。例如,如果返回五首歌曲并且各个曲目 ID 分别为 111、222、333、444 和 555,那么我想要一个数组,比如 "trackids" 为 trackids=[111, 222, 333, 444,和 555]。我正在考虑两种方法;对特定列执行类似 get getelementbyId/tag 的操作,或者将 song.trackId 数据直接发送到 "trackids" 数组。这是代码:
<body>
<div ng-controller="iTunesController">
{{ error }}
<form name="search" ng-submit="searchiTunes(artist)">
<input type="search" required placeholder="Artist or Song" ng-model="artist" />
<input type="submit" value="Search" />
</form>
<div class="element"></div>
<table id="SongInfo" ng-show="songs">
<thead>
<tr>
<th>Album Artwork</th>
<th>Track</th>
<th>Artist</th>
<th></th>
<th>Track Id</th>
<th>Preview</th>
<th>Track Info</th>
<th>Track Price</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="song in songs">
<td><img ng-src="{{song.artworkUrl60}}" alt="{{song.collectionName}}" />
</td>
<td>{{song.trackName}}</td>
<td>{{song.artistName}}</td>
<td>
<button ng-click="handleAdd(song)">Add Track</button>
</td>
<td>{{song.trackId}}</td>
<td><a href="{{song.previewUrl}}">Play</a></td>
<td><a href="{{song.trackViewUrl}}">View Track Info</a></td>
<td>{{song.trackPrice}}</td>
</tr>
</tbody>
</table>
</div>
</body>
itunes_controller.js
(function() {
angular.module('app', [])
.controller('iTunesController', function($scope, $http) {
$scope.searchiTunes = function(artist) {
$http.jsonp('http://itunes.apple.com/search', {
params: {
'callback': 'JSON_CALLBACK',
'term': artist,
limit: 5,
}
}).then(onSearchComplete, onError)
}
var onSearchComplete = function(response) {
$scope.data = response.data
$scope.songs = response.data.results
}
var onError = function(reason) {
$scope.error = reason
}
});
}())
感谢任何帮助并提前致谢!!
在第一次返回数据时只获取 ID 怎么样?
var onSearchComplete = function(response) {
$scope.data = response.data
$scope.songs = response.data.results
$scope.trackIds = [];
angular.forEach($scope.songs, function(song) {
$scope.trackIds.push(song.trackId)
}) // trackIds are now stored in $scope.trackIds
}
在表格中搜索艺术家或歌曲时,会返回 table 首歌曲,其中的列标题是与歌曲相关的信息描述。我的目标是仅将 "Track Id" 列值发送到数组以供以后使用。例如,如果返回五首歌曲并且各个曲目 ID 分别为 111、222、333、444 和 555,那么我想要一个数组,比如 "trackids" 为 trackids=[111, 222, 333, 444,和 555]。我正在考虑两种方法;对特定列执行类似 get getelementbyId/tag 的操作,或者将 song.trackId 数据直接发送到 "trackids" 数组。这是代码:
<body>
<div ng-controller="iTunesController">
{{ error }}
<form name="search" ng-submit="searchiTunes(artist)">
<input type="search" required placeholder="Artist or Song" ng-model="artist" />
<input type="submit" value="Search" />
</form>
<div class="element"></div>
<table id="SongInfo" ng-show="songs">
<thead>
<tr>
<th>Album Artwork</th>
<th>Track</th>
<th>Artist</th>
<th></th>
<th>Track Id</th>
<th>Preview</th>
<th>Track Info</th>
<th>Track Price</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="song in songs">
<td><img ng-src="{{song.artworkUrl60}}" alt="{{song.collectionName}}" />
</td>
<td>{{song.trackName}}</td>
<td>{{song.artistName}}</td>
<td>
<button ng-click="handleAdd(song)">Add Track</button>
</td>
<td>{{song.trackId}}</td>
<td><a href="{{song.previewUrl}}">Play</a></td>
<td><a href="{{song.trackViewUrl}}">View Track Info</a></td>
<td>{{song.trackPrice}}</td>
</tr>
</tbody>
</table>
</div>
</body>
itunes_controller.js
(function() {
angular.module('app', [])
.controller('iTunesController', function($scope, $http) {
$scope.searchiTunes = function(artist) {
$http.jsonp('http://itunes.apple.com/search', {
params: {
'callback': 'JSON_CALLBACK',
'term': artist,
limit: 5,
}
}).then(onSearchComplete, onError)
}
var onSearchComplete = function(response) {
$scope.data = response.data
$scope.songs = response.data.results
}
var onError = function(reason) {
$scope.error = reason
}
});
}())
感谢任何帮助并提前致谢!!
在第一次返回数据时只获取 ID 怎么样?
var onSearchComplete = function(response) {
$scope.data = response.data
$scope.songs = response.data.results
$scope.trackIds = [];
angular.forEach($scope.songs, function(song) {
$scope.trackIds.push(song.trackId)
}) // trackIds are now stored in $scope.trackIds
}