2 回答
TA贡献1851条经验 获得超3个赞
解决方案1
HTML:
<select class="form-control" id="gameList" ng-model="game" ng-change="getPlayers()">
<option ng-repeat="game in allGames" ng-value="{{game.id}}"> {{game.name}}</option>
</select>
在 getPlayers() 中
var url = apiUrl + "/game/" + $scope.game;
解决方案2:
HTML:
<select class="form-control" id="gameList" ng-model="game" ng-change="getPlayers()">
<option ng-repeat="game in allGames" ng-value="{{game}}"> {{game.name}}</option>
</select>
在 getPlayers() 中
var url = apiUrl + "/game/" + $scope.game.id;
运行以下代码片段
angular
.module('myapp',[])
.controller('ctrl', function($scope){
$scope.game;
$scope.allGames=[
{
name:"GTA",
id:1
},
{
name:"PUBG",
id:2
}
];
$scope.getPlayers= function () {
var url = "apiUrl/game/" + $scope.game.id;
console.log(url);
}});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
<div ng-app="myapp" class="card-body">
<div ng-controller="ctrl" class="form-group">
<label class="col-md-3 col-form-label">List of Games</label>
<select class="form-control" id="gameList" ng-model="game" ng-change="getPlayers()">
<option ng-repeat="game in allGames" ng-value="game"> {{game.name}}</option>
</select>
</div>
</div>
TA贡献1848条经验 获得超10个赞
将此处的 ng-value 从 game 更改为 game.id
<div class="card-body">
<div class="form-group">
<label class="col-md-3 col-form-label">List of Games</label>
<select class="form-control" id="gameList" ng-model="game" ng-change="getPlayers()">
<option ng-repeat="game in allGames" ng-value="{{game.id}}"> {{game.name}}</option>
</select>
</div>
在js代码中定义$scope.game;
$scope.game;
$scope.getPlayers = function () {
var url = apiUrl + "/game/" + $scope.game.id;
$http.get(url, config)
.then(function (response) {
if (response.data) {
$scope.players = response.data;
} else {
$scope.showErrorMessage("Player not found");
}
},
function (error) {
$scope.responseErrorCheck(error);
}
);
};
- 2 回答
- 0 关注
- 78 浏览
添加回答
举报