2016-12-15 10 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が与えられた状態ではなく、デフォルトオプションが表示されます。

次のハックは解決策を提供します。このコードは、$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

場合は、この 'IF($のscope.customer.state){ //$scope.customer.state++ようにそれを行うことができます。 //$scope.customer.state--; $ scope.getStatesContentData($ scope.customer.state) } ' –

関連する問題