【问题标题】:execute function after ng-repeat finishes with hidden elements在 ng-repeat 完成隐藏元素后执行函数
【发布时间】:2014-04-23 16:48:38
【问题描述】:

这是情况,我有这个代码

<div data-ng-repeat="question in questions" data-ng-if="cQuestion == $index + 1" class="question-animate">
  {{question.question}}-{{$index + 1}}
  <div data-ng-repeat="answer in question.answers">
    {{answer}}&nbsp
  </div>
  <input type="button" value="next" data-ng-click="nextQuestion()"/>
</div>

我想检测 ng-repeat 何时结束,以便我可以更新 cQuestion 以显示第一个问题。由于我们有data-ng-if="cQuestion == $index + 1",这首先评估为 false,并且未显示该元素,这限制了我检测 ng-repeat 何时完成的选项。我尝试过使用指令,但由于未显示该元素并且未触发该指令。 ng-if 之所以存在,是因为重复的对象上有动画。 现在我用 setTimeout 来做,但我认为这种技术不可靠。

【问题讨论】:

  • 所以你想显示第一个问题,点击按钮,它应该显示下一个,对吗?

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

让我们将索引cQuestion 移出视图。

您的控制器应该完成这项工作。 创建一个变量currentQuestion,它代表当前显示的问题。

<div>
  {{currentQuestion.question}}-{{currentQuestionIndex+1}}
  <div data-ng-repeat="answer in currentQuestion.answers">
    {{answer}}&nbsp
  </div>
  <input type="button" value="next" data-ng-click="nextQuestion()"/>
</div>

然后确保 nextQuestion 确实改变了当前问题:

function nextQuestion(){
    $scope.currentQuestion = $scope.questions[$scope.currentQuestionIndex];
    $scope.currentQuestionIndex ++; //init with 0
}

不要忘记在控制器开始时用第一个问题初始化$scope.currentQuestion。

【讨论】:

  • 10x :) 这工作正常,但现在我无法触发动画,我只是将第一个开头的
    更改为
    我有一个与问题动画挂钩的动画,但由于某种原因它没有触发,它只是出现了。你能帮我吗?
猜你喜欢
相关资源
最近更新 更多
热门标签