2016-04-15 4 views
0

次の角度コントローラがあります。私はxyの両方の変数から項目を取り出し、それらをウェブページに表示したいと思います。現在、ウェブページにはy変数の項目のみが表示されます。 .push()を使用して、2つの関数結果セットをまとめてPremByClass配列に入れようとします。これを行う方法に関する助言や指示があれば大いに感謝します。Angular.jsまたはJavaScriptで2つの関数リターンを結合しようとしています

app.controller('PremByClass', ['$scope', '$http', 'policyApi', 'lookupApi', function ($scope, $http, policyApi, lookupApi) { 
    $scope.model = {}; 
    $scope.load.then(function() { 
     function formatDate(value) { 
      return value.getMonth() + 1 + "-" + value.getDate() + "-" + value.getFullYear(); 
     } 
     $scope.PremByClass = []; 
     policyApi.policy.get().then(function (t) { 
      if (t) { 
       policyApi.class.get().then(function (x) { 
        if (x) { 
         //Put x into $scope.PremByClass 
         $scope.PremByClass.push(x); 
         var dtEff = formatDate(t.DateEffective); 
         for (var i = 0, e = null; e = x[i]; i++) { 
          lookupApi.WCClassDesc.get(e.GovState, e.classCode, e.DescCode, dtEff).then(function (y) { 
           //This seems to wrok correctly however it seems that $scope.PremByClass forgets about x 
           //The web page only shows stuff from y 
           $scope.PremByClass.push(y); 
          }) 
         } 
        } 
       }) 
      } 
     }) 
    }); 
}]); 

答えて

0

それはあなたのX変数が配列であると思われたように、私のようなものを持つオブジェクトのリストやテーブルなど、あなたのページでそれを示すべきであると思います。私が使用

<ul> 
    <li ng-repeat="item in PremByClass[0]"> 
    {{item.GovState}} // {{item.classCode}} 
    </li> 
</ul> 

x変数がPremByClass配列にプッシュする最初の項目であるため、PremByClassのインデックス0。しかし、x変数をより意味のあるスコーププロパティに設定することで、コードのその部分をよりメンテナンス可能にすることができます。

あなたのコントローラでは、この行を置き換えることができます:この行で

$scope.PremByClass.push(x); 

を:その後、

$scope.policies = x; 

と単にポリシーを使用するようにngのリピートブロックを書き換える:

<ul> 
    <li ng-repeat="item in policies"> 
    {{item.GovState}} // {{item.classCode}} 
    </li> 
</ul> 

変数/配列PremByClassは、テインは、あなたのlookupApi.WCClassDescサービスや工場から生じ、あなたは単にPremByClassポリシーを交換し、上記と同じNGリピートコードを使用して画面上に描画することができるはずです。

関連する問題