2015-04-05 75 views
1

我有我的模塊中的以下數組中Angular.js採用NG-重複表格格式

$scope.hsbody = []; //Data array 
$scope.hsresult = []; //Data array 
$scope.hsProcess = []; //Boolean array 
$scope.hssuccess = []; //Boolean array 
$scope.hsfailure = []; //Boolean array 
$scope.hsExpand = []; //Boolean array 
$scope.hsExpandUser = []; //Boolean array 

我想表明我的HTML頁面數組項:

hsresult 
hsbody 
hsresult 
hsbody 
and so on.. 

所以我做以下內容:

<div> 
     <pre> 
      <table class="table table-condensed"> 
        <tr ng-repeat="hs in hsbody track by $i" ng-show="hsProcess[i] && !hssuccess[i] && !hsfailure[i]" class="warning"><td><div class="glyphicon"></div>{{hsbody}}</td></tr> 
        <tr ng-show="hssuccess" ng-repeat="highstate in hsbody track by $i" class="success"><td><div class="glyphicon" ng-show="!hsExpand[i]"></div><div class="glyphicon" ng-show="hsExpand[i]"></div>{{ hsresult[i] }} </td></tr> 
        <tr ng-show="hsfailure" ng-repeat="hs in hsbody track by $i" class="danger"><td><div class="glyphicon" ng-show="!hsExpand"></div><div class="glyphicon" ng-show="hsExpand[i]"></div>{{ hsresult[i] }}</td></tr> 
        <tr ng-repeat="hs in hsbody track by $i" ng-show="(hsProcess[i] && hsExpand[i]) || (hsExpand[i] && hsfailure[i])" class="active"><td><pre>{{ hsbody[i] }}</pre></td></tr> 
      </table> 
     </pre> 
    </div> 

問題是我的HTML中沒有顯示任何內容。但是當我擺脫ng-repeat並使用i=0時,我可以看到這些值。

看來我沒有正確使用ng-repeat,但我不知道我錯在哪裏。

+0

whats'$ i' in track by? – 2015-04-05 06:43:27

+0

@ K.Toress:你能澄清一下這個問題嗎?我所要做的就是將所有物品放入hsbody中,並使用'$ i'索引來遍歷它們。我知道'hsbody.length = hsresult.result = hsProcess.length =等等,所以我使用每個數組的'$ i'值。 – MiddleWare 2015-04-05 06:56:44

+0

爲什麼你不恰當地重組控制器中的數據?這就是我認爲你應該做的。 – Wio 2015-04-05 07:01:27

回答

0

在'ng-repeat'的內部,你必須通過你在ng-repeat中指定的任何東西來引用所有的東西。

例如假設你在控制器的「數據」中有一個數組'main'。

你有對象的用戶名data.main內

$scope.main = [ 
    {username: 'jeff'}, 
    {username: 'brad'}, 
] 

this.main = [ 
    {username: 'jeff'}, 
    {username: 'brad'}, 
] 

如果你寫

<div ng-repeat="theData in data.main track by $i"> 
    {{ data.main.username }} 
</div> 

錯誤會出現,因爲它是不確定的。

如果你寫

<div ng-repeat="theData in data.main track by $i"> 
    {{ theData.username }} 
</div> 

你會得到你想要的東西。

原因是你指定data作爲ng-repeat內的所有東西的來源。

海圖相當於data.main,爲NG-重複

無法對象data.main外部訪問DIV中的任何內部。

如果你需要從外面的任何東西,再想一想。你真的很想多次打印這些數據嗎?

+0

如果它不起作用,請用'$ index'跟蹤它。你可能需要在你的控制器的頂部添加'$ index'到$'scope' app.controller('ExapmleCtrl',function($ scope,$ index){...});' – Tobsta 2015-04-05 07:14:30

0

如果你想使用帶有索引的「track by」,你應該在那裏寫上:「track by $ index」。 祝你好運!

+0

我改變了所有我發生的索引,但是相同 – MiddleWare 2015-04-05 07:08:08

0

這適合你嗎?

<div> 
    <pre> 
     <table class="table table-condensed"> 
      <tr ng-repeat="hs in hsbody track by $index" ng-show="hsProcess[$index] && !hssuccess[$index] && !hsfailure[$index]" class="warning"> 
       <td> 
        <div class="glyphicon"></div>{{hs}} 
       </td> 
      </tr> 
      <tr ng-show="hssuccess" ng-repeat="highstate in hsbody track by $index" class="success"> 
       <td> 
        <div class="glyphicon" ng-show="!hsExpand[i]"></div> 
        <div class="glyphicon" ng-show="hsExpand[$index]"></div>{{ hsresult[$index] }} 
       </td> 
      </tr> 
      <tr ng-show="hsfailure" ng-repeat="hs in hsbody track by $index" class="danger"> 
       <td> 
        <div class="glyphicon" ng-show="!hsExpand"></div>  
        <div class="glyphicon" ng-show="hsExpand[i]"></div>{{ hsresult[$index] }} 
       </td> 
      </tr> 
      <tr ng-repeat="hs in hsbody track by $index" ng-show="(hsProcess[$index] && hsExpand[$index]) || (hsExpand[$index] && hsfailure[$index])" class="active"> 
       <td> 
        <pre>{{ hsbody[$index] }}</pre> 
       </td> 
      </tr> 
     </table> 
    </pre> 
</div>