堆棧溢出與填充下拉菜單相關的很多解決方案包括$scope
。
我的第二個下拉菜單取決於我的第一個下拉菜單的值,因此我在第一個HTML select
上使用ng-change
將參數傳遞給第二個下拉菜單的函數。
1下拉HTML和角度JS:
<select data-ng-controller="addAssetController as addAssetCtrl" id="functionalOrg" data-ng-model="addAssetFormCtrl.functionalOrg.id" ng-change="addAssetCtrl.getLocations(addAssetFormCtrl.functionalOrg.id)">
<option data-ng-repeat="functionalOrg in addAssetCtrl.functionalOrgs | orderBy:'id' track by $index" value="{{functionalOrg.id}}">
{{functionalOrg.id}} - {{functionalOrg.account}}
</option>
</select>
因此ng-change
:
ng-change="addAssetCtrl.getLocations(addAssetFormCtrl.functionalOrg.id)"
-
var vm = this;
functionalOrganisationRepository.getFunctionalOrganisation().then(function (results) {
vm.functionalOrgs = results;
}, function (error) {
vm.error = true;
vm.errorMessage = error;
});
的第二個下拉HTML和角度:
<select data-ng-controller="addAssetController as addAssetCtrl" id="location" data-ng-model="addAssetFormCtrl.location.id">
<option data-ng-repeat="location in addAssetCtrl.locations | orderBy:'id' track by $index" value="{{location.id}}">
{{location.id}} - {{location.address6}}
</option>
</select>
-
vm.getLocations = function(id) {
console.log("Functional org ID:" + id);
locationRepository.getLocation(id).then(function (results) {
vm.locations = results;
}, function (error) {
vm.error = true;
vm.errorMessage = error;
});
}
假設我的業務層是罰款並帶回與我需要的一切JSON對象,什麼可能的問題是什麼?由於我的控制檯日誌正在打印,因此vm.getLocations
函數肯定會被調用。服務層也很好,因爲要記錄到我的命令提示符的JSON對象。
我的問題是我如何填充我的第二下拉從任何JSON返回getLocations
?因此,我不想在Angular中使用$scope
。
請創建一個plunker並提供 –
方法locationRepository.getLocation(id)檢索列表? –
每次選擇時都會重複「ng-controller」屬性。只將屬性放在父元素上一次! – manzapanza