我需要您对AngularJS问题的帮助:我希望基于其他HTML select
获得HTML select
(这是一个非常常见的网络问题)。我用这种方式完成了这项任务:
查看:
<div class="form-group">
<select data-ng-change="getOffices()" data-ng-model="selectedCustomer" data-ng-options="customer.name for customer in customers" class="form-control">
<option value="">Choose customer...</option>
</select>
<select class="form-control">
<option value="">Choose office...</option>
<option data-ng-repeat="office in selectedCustomer.offices.getAll">{{office.name}}</option>
</select>
</div>
控制器:
angular.module("app").controller("receiptsController", ["$scope",
function ($scope){
//selected customer
$scope.selectedCustomer = null;
//get offices related to the selected customer
$scope.getOffices = function(){
if($scope.selectedCustomer !== null)
$scope.selectedCustomer.getOffices();
};
}]);
这很正常,但我想了解为什么以下解决方案无法正常工作。假设控制器相同,视图变为:
<div class="form-group">
<select data-ng-change="getOffices()" data-ng-model="selectedCustomer" data-ng-options="customer.name for customer in customers" class="form-control">
<option value="">Choose customer...</option>
</select>
<select class="form-control" data-ng-options="office.name for office in selectedCustomer.offices.getAll">
<option value="">Choose office...</option>
</select>
</div>
在这种情况下(在两个选择中都使用data-ng-options
指令),第二个select
未填充。有人能解释一下为什么吗?先感谢您!
编辑:方法getOffices()
是一个AJAX请求:
angular.module("app").factory("GetOffices", ["$resource",
function($resource){
return $resource("../retrieveOffices.php",
{customerID:'@id'});
}]);
答案 0 :(得分:0)
您需要这样做,请参阅$ resource
上的角度文档 angular.module("app").controller("receiptsController", ["$scope",GetOffices
function ($scope,GetOffices){
//selected customer
$scope.selectedCustomer = null;
//get offices related to the selected customer
$scope.getOffices = function(){
if($scope.selectedCustomer !== null)
$scope.selectedCustomer=GetOffices.get({customerID:123});
};
}]);