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 關注
- 117 瀏覽
添加回答
舉報