2015-01-08 34 views
0

切換嵌套式中繼器中元素的最佳做法是什麼?我有以下的角模板:angularjs在嵌套式中繼器中顯示或隱藏

<div> 
    {{question.Description}} 
    <div ng-repeat="answer in question.Answers"> 
     <a href="#" ng-click="answerQuestion(question, answer.ID, $index)">{{answer.Description}}</a> 
     <div ng-repeat="qAnswer in answer.Questions"><!-- Hide/show here --> 
      <a href="#" ng-click="answerQuestion(question, qAnswer.ID, $index)">{{qAnswer.Description}}</a> 
      <div ng-repeat="childAnswer in qAnswer.Answers"><!-- Hide/show here --> 
       <a href="#" ng-click="answerQuestion(question, childAnswer.ID, $index)">{{childAnswer.Description}}</a> 
      </div> 
     </div> 
    </div> 
</div> 

編輯:

我想來切換基於點擊動作

+1

切換什麼,基於什麼標準呢?再次閱讀您的問題 - 感覺完整嗎? –

回答

1
<div> 
    {{question.Description}} 
    <div ng-repeat="answer in question.Answers"> 
     <a href="#" ng-click="answerQuestion(question, answer.ID, $index);answer.show=!answer.show">{{answer.Description}}</a> 
     <div ng-repeat="qAnswer in answer.Questions" ng-show="answer.show"><!-- Hide/show here --> 
      <a href="#" ng-click="answerQuestion(question, qAnswer.ID, $index);qAnswer.show=!qAnswer.show">{{qAnswer.Description}}</a> 
      <div ng-repeat="childAnswer in qAnswer.Answers" ng-show="qAnswer.show"><!-- Hide/show here --> 
       <a href="#" ng-click="answerQuestion(question, childAnswer.ID, $index)">{{childAnswer.Description}}</a> 
      </div> 
     </div> 
    </div> 
</div> 

u需要的對象值設置爲true或false,那麼angularjs會完成剩下的工作。

1

嘗試這樣的事情,

<div ng-init="items_display=[]"> 
    {{question.Description}} 
    <div ng-repeat="answer in question.Answers"> 
     <a href="#" ng-click="answerQuestion(question, answer.ID, $index)">{{answer.Description}}</a> 
     <div ng-repeat="qAnswer in answer.Questions"><!-- Hide/show here --> 
      <a href="#" ng-click="answerQuestion(question, qAnswer.ID, $index)" ng-click="items_display[$index] = !items_display[$index]">{{qAnswer.Description}}</a> 
      <div ng-repeat="childAnswer in qAnswer.Answers" ng-show="items_display[$index]"><!-- Hide/show here --> 
       <a href="#" ng-click="answerQuestion(question, childAnswer.ID, $index)">{{childAnswer.Description}}</a> 
      </div> 
     </div> 
    </div> 
</div>