【发布时间】:2017-06-19 23:23:21
【问题描述】:
这是我请求在服务器上发布数据的控制器。虽然我可以发布标题和评级,但我无法发布任何类型,因为 Genres 是一个数组。如何推送 Genres Array 中的数据?
app.controller('addTrack', function ($scope, $http) {
$scope.tracks = [];
var genre = [];
var pushing = genre.push($scope.add_genre);
$scope.add_track = "";
$http.get('http://104.197.128.152:8000/v1/tracks?page=48')
.then(function (response) {
$scope.tracks = response.data.results;
});
$scope.add = function (event) {
if (event.keyCode == 13) {
$http.post('http://104.197.128.152:8000/v1/tracks', {
title: $scope.add_title,
rating: $scope.add_rating,
})
.then(function (data) {
$scope.genres = data;
//console.log($scope.add_genre);
$scope.add_title = '';
$scope.add_rating = '';
$scope.add_genre = '';
});
$http.get('http://104.197.128.152:8000/v1/tracks?page=48')
.then(function (response) {
$scope.genres = response.data.results;
});
}
} });
Http POST 到http://104.197.128.152:8000/v1/tracks 接受回复
{
"title": "animals",
"rating": 4.5,
"genres": [
1
]
}
也为您提供我的 HTML。
<!DOCTYPE html>
<html ng-app="musicApp">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div class="container" ng-controller="addTrack">
<div>
<h1>Add New Track</h1>
</div>
<div class="form-group">
<label>Title:</label>
<input type="text" class="form-control" ng-model="add_title" placeholder="Type to add new Title">
</div>
<div class="form-group">
<label>Genre:</label>
<input type="text" class="form-control" ng-model="add_genre" placeholder="Type to add new Genre">
</div>
<div class="form-group">
<label>Rating:</label>
<input type="text" class="form-control" ng-model="add_rating" placeholder="Type to add new Rating" ng-keyup="add($event)">
</div>
<div class="clearfix">
<button class="btn btn-primary col-xs-12 bottom-button" value="click" id="button">Add a New Track</button>
</div>
<div class="clearfix">
<label>Available Tracks</label>
<ul class="list-group" ng-repeat="track in tracks">
<li class="list-group-item clearfix"><span class="pull-left title">{{track.title}}</span> <span class="genre">[{{track.genres[0].name}}]</span> <span class="pull-right rating">{{track.rating}}</span></li>
</ul>
</div>
</div>
</body>
</html>
【问题讨论】:
-
首先你需要在你的控制器中声明
$scope.genres,然后使用这个代码$scope.genres = response.data.results;,你正在覆盖数组,你需要像这样使用.push():$scope.genres.push(response.data.results);,我不明白!一首曲目应该只有一个genre不是吗? -
@chsdk 哈哈,我同意!提供给我的后端代码非常模糊。你能帮我写代码吗?我无法将数据推送到流派数组。
-
你能更好地解释一下你的场景吗?为什么要为流派使用数组?每首曲目不应该是一种类型吗?
-
是的,应该只有一个流派,但在我的任务中我必须添加超过 1 个。我的任务是添加一个曲目。为此,我需要添加 Title、Rating 和 Genre。到目前为止,我已经能够添加标题和评分,但我无法添加流派。
-
那么在这种情况下,您需要更改代码以绑定到 atrack
object并更改其属性。我会尽力提供答案。
标签: javascript angularjs arrays json