2016-04-15 55 views
0

我有以下Angular控制器。我想从xy变量中获取物品并将其显示在网页上。目前,该网页仅显示y变量中的项目。我尝试使用.push()将两个函数结果集放在一起到PremByClass数组中。任何帮助或方向如何做到这一点将不胜感激。试图在Angular.js或JavaScript中加入两个函数返回值

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> 

我用PremByClass上的索引0,因为您的x变量是您推入PremByClass数组的第一项。尽管如此,通过将x变量设置为更有意义的范围属性,我会使代码的部分更易于维护。

你可以在你的控制器替换该行:

$scope.PremByClass.push(x); 

这一行:

$scope.policies = x; 

,然后简单地重写你的NG-重复块使用政策

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

变量/数组PremByClass然后会con覃仅因您的lookupApi.WCClassDesc服务或工厂,你应该能够使用相同的NG-重复码如上简单地PremByClass更换政策,使它们在屏幕上。