用ajax加载选项选择值

用ajax加载选项选择值

问题描述:

我有一个客户对象在我的范围内有一个国家和一个国家(如ID)。 某些国家要求用户选择一种状态。因此,如果选择其中一个国家,则会将状态加载到对象中,并显示状态下拉菜单。用ajax加载选项选择值

<select ng-model="customer.country" ng-init="getStatesContentData(customer.country)" 
    ng-change="getStatesContentData(customer.country)" ng-options="country.isocode as country.name for country in countries" required> 

<select ng-model="customer.state" ng-options="state.id as state.name for state in states[customer.country]"> 

getStatesContentData(country)执行一个Ajax请求,并把该国$scope.states[country]

现在的问题是,如果我加载站点作为登录的用户(=在我的客户对象定状态)显示默认选项,而不是给出id的状态。

以下hack提供了一个解决方案。该代码在$scope.states[country]填充后执行。

if($scope.customer.state){ 
    $scope.customer.state++; 
    $scope.customer.state--; 
} 

有没有更好的方法来做到这一点?

+0

尝试用$()。如果它不起作用创建一个小提琴。 – selvassn

+0

我已经试过'$ scope.apply()',但没有运气。 – Strernd

是scope.apply试试这个

if($scope.customer.state){ 
    //$scope.customer.state++; 
    //$scope.customer.state--; 
    $scope.getStatesContentData($scope.customer.state) 
} 
+0

这绝对不行。如前所述,'getStatesContentData(country)'方法接受一个国家而不是一个国家。 – Strernd

+0

如果已经在用户那么你有他的国家在 你可以把它像这样 '如果($ scope.customer.state){ //$scope.customer.state++; //$scope.customer.state--; $ scope.getStatesContentData($ scope.customer.state) }' –