我有以下的JSON文件,其中列出的國家和他們的狀態:AngularJS - 如何使級聯選擇並保存到API
[
{
"value": "Australia",
"text": "Australia",
"states": [
{
"value": "Australian Capital Territory",
"text": "Australian Capital Territory"
},
{
"value": "New South Wales",
"text": "New South Wales"
}
]
}
]
我在一個變量稱爲countriesAndStates加載數據和然後我在前端顯示此:
<div class="form-group">
<label for="country" class="col-sm-2 control-label">Country</label>
<div class="col-sm-3">
<select class="form-control" id="country" data-ng-model="project.country" data-ng-options="country.value as country.text for country in countriesAndStates">
<option disabled value="">Select</option>
</select>
</div>
</div>
<div class="form-group">
<label for="country" class="col-sm-2 control-label">State</label>
<div class="col-sm-3">
<select class="form-control" id="state" data-ng-model="project.state" data-ng-options="state.value as state.text for state in countriesAndStates.states" data-ng-disabled="!countriesAndStates.states">
<option disabled value="">Select</option>
</select>
</div>
</div>
我也從一個REST API加載數據並將其顯示在前端:
var detailProject = Restangular.one('projects', $routeParams.projectId);
detailProject.get().then(function (project) {
$scope.project = project;
$scope.saveProject = function() {
project.put().then(processSuccess, processError);
};
});
當我點擊保存,一saveProject函數的數據提出回API。
現在......我如何將國家與國家聯繫起來?
從我的文檔中理解的,我需要改變NG-模特屬性,以反映國家和國家......像這樣:
<select id="country" ng-model="states" ng-options=""></select>
<select id="state" ng-disabled="!states" ng-options=""></select>
但是我有點糊塗了,怎麼能當我想要將API數據加載到選擇框中時,我這樣做了嗎?
人有什麼想法? – eHx 2014-11-24 19:37:30