我有一個本地$ scope.countries在我的HTML填充國家州城市,但不是選擇作爲默認值在下拉列表中,我需要顯示國家州城市值來自後端作爲默認設置。例如,阿富汗,巴達謝山。如何分配默認值從後端迴應下拉
Plunker代碼在這裏。 http://plnkr.co/edit/DPoOFRKGXO28tDXzGe5B?p=preview
<html lang="en-US">
<head>
<meta charset="utf-8">
<link href="css/custom.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
</head>
<body ng-controller="MainCtrl">
<form action="#" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<div class="form-group">
<div class="col-sm-4">
<select name="country" ng-model="model.country" class="form-control countries" id="countryId" required="required">
<option value="">Select Country</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<select name="state" ng-model="model.state" class="form-control states" id="stateId" required="required">
<option value="">Select State</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<select name="city" ng-model="model.city" class="form-control cities" id="cityId" required="required">
<option value="">Select City</option>
</select>
</div>
</div>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="app.js"></script>
<script src="location.js"></script>
</body>
</html>
JS:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
var response = {
"response":
{"json":
{"session_id":"498",
"profile":{"country":"Afghanistan",
"state":"Badakhshan","city":"Eshkashem",
"pincode":"54564",
"rolemandatory":1},
"roles":[]}}}
$scope.getProfile = function() {
$scope.model.country = response.response.json.profile.country;
$scope.model.state = response.response.json.profile.state;
$scope.model.city = response.response.json.profile.city;
};
});
您的重擊器有很多更改!很抱歉地說,但爲什麼地球上你使用JQuery將Ajax結果綁定到下拉菜單?爲什麼不使用$ scope/ng-options? –
你能分享我一些例子嗎? –
http://stackoverflow.com/a/39716944/4316707看看這個答案它有一個plunker演示如何使用 –