2016-12-15 91 views
0

我有一個客戶對象在我的範圍內有一個國家和一個國家(如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

回答

0

是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) }' –