2017-03-21 6 views
0

前に呼び出され、私はコントローラNG-initをAPIレスポンス

this.getStates = function() { 
       LoadingService.enableLoading(); 
       CartService.getStates(self.customerDetails.Country).then(function (response) { 
        response = response.data; 
        self.states = response.State; 
        LoadingService.disableLoading(); 
       }).catch(function (response) { 
        console.log(response); 
        LoadingService.disableLoading(); 
       }); 
      }; 

にこのような機能を持っており、私の見解では、それはこのように、ここで

をレンダリングするには、テンプレートコードは

です
<select required id='country' class="form-control" ng-model="checkoutCntrl.customerDetails.Country" ng-init="checkoutCntrl.customerDetails.Country = checkoutCntrl.countries[107]" 
       ng-options="item.CountryCode as item.CountryName for item in checkoutCntrl.countries track by item.CountryCode" 
       ng-select="checkoutCntrl.getStates()" ng-change="checkoutCntrl.getStates()"> 
     </select> 

どのように選択すればデフォルトの国を選択したかったのですか?私は、NG-のinit内の機能を使用するときに呼び出され、我々は、サーバーからデータを取得する前に、ここで

は状態

self.customerDetails.Countryがある
this.getStates = function() { 
       console.log('obj=====>'); 
       console.log(self.customerDetails.Country.CountryCode); 
       LoadingService.enableLoading(); 
       CartService.getStates(self.customerDetails.Country).then(function (response) { 
        response = response.data; 
        self.states = response.State; 
        LoadingService.disableLoading(); 
       }).catch(function (response) { 
        console.log(response); 
        LoadingService.disableLoading(); 
       }); 
      }; 

を取得しようとしている国の値を設定した後、次のコードであり、オブジェクトとして来る

+1

テンプレートコードを表示するのを助けてください。 – WorkWe

答えて

0

ng-initは式を1回だけ評価します。あなたのリクエストは非同期であるため、リクエストが成功するまでにng-initはすでに評価されています。あなたの変数を初期化するためのより良い方法は、あなたの成功コールバックでこれを書くことであろう

checkoutCntrl.customerDetails.Country = checkoutCntrl.countries[107]; 

そして、あなたはあなたの選択は、要求の前に表示させたくない場合は、ときにのみディレクティブをレンダリングするためにNG-場合は使用を検討してくださいあなたの状態は満たされます。たとえば、

<div ng-if="showMeOnSuccessfulRequest" >Content...</div> 

CartService.getStates(self.customerDetails.Country).then(function (response) { 
       response = response.data; 
       self.states = response.State; 
       $scope.showMeOnSuccessfulRequest = true; 
       LoadingService.disableLoading(); 
      }).catch(function (response) { 
       console.log(response); 
       LoadingService.disableLoading(); 
      }); 
+0

私がこれを好きなら、それは値を設定していますが、私がそれに取り込もうとしたときに価値の代わりにオブジェクトとして来ています。私が何か他のものを選択した場合は、オブジェクト –

+0

の代わりに値を送信するだけで、コントローラから選択した後に別のオプションを選択すると空のオプションが選択されるという別の問題があります –

+0

作業中のシナリオあなたがそれをより良く理解するのに役立つ適切な例を提供することができます。私の選択データは –

関連する問題