我正在使用Angular的ng-show指令来改变问题在测验中的显示方式,然后在测验完成时显示结果。除了第一个问题,一切都按预期工作。当测验加载时,我初始化$ scope.image = false。出于某种原因,出现了带有ng-show =“image”的div和带有ng-show =“!image”的div。我希望只出现带有ng-show =“!image”的div。相关代码如下。注意:为了节省空间,我没有在控制器或index.html中包含我的所有问题或结果对象,其中包含正确的ng-app标题等:
HTML:
<div class="jumbotron text-center" ng-hide="quizComplete" ng-show="!image">
<h3>{{ questions[number].ask }}</h3>
<div class="row">
<div ng-repeat="answer in questions[number].answers" class="choice">
<div ng-click="recordChoice(answer.points)">
<span class="centerer"></span>
<span class="centered">{{ answer.choice }}</span>
</div>
</div>
</div>
</div>
<div class="jumbotron text-center" ng-hide="quizComplete" ng-show="image">
<h3>{{ questions[number].ask }}</h3>
<div class="row">
<div ng-repeat="answer in questions[number].answers" class="choice">
<div ng-click="recordChoice(answer.points)">
<img ng-src="/img/{{ answer.img }}.jpg" alt="" class="img-responsive">
</div>
</div>
</div>
</div>
<div class="jumbotron text-center" ng-show="quizComplete">
<h2>{{ results[result].title }}</h2>
<p>{{ results[result].details }}</p>
</div>
控制器:
angular.module('NerdCtrl', []).controller('NerdController', function($scope, $routeParams) {
$scope.questions = [{"ask": "Choose a Social Media platform:", "answers": [{"choice": "Facebook", "points": 2, "img": "Facebook"}, {"choice": "Twitter", "points": 3, "img": "Twitter"}, {"choice": "Google+", "points": 1, "img": "GooglePlus"}]}];
$scope.results = [{title: "The Mummy", details: "You are the original..."}];
$scope.number = 0;
$scope.tallyMummy = 0;
$scope.tallyFrankenstein = 0;
$scope.tallyWolfman = 0;
$scope.tallyJeckyll = 0;
$scope.image = false;
$scope.quizComplete = false;
$scope.recordChoice = function(points) {
if ($scope.number < 9) {
switch(points) {
case 1:
$scope.tallyMummy++;
break;
case 2:
$scope.tallyFrankenstein++;
break;
case 3:
$scope.tallyWolfman++;
break;
case 4:
$scope.tallyJeckyll++;
break;
}
$scope.number++;
if ($scope.number === 1 || $scope.number === 6 || $scope.number === 7 || $scope.number === 9) {
$scope.image = true;
} else {
$scope.image = false;
}
} else {
$scope.quizComplete = true;
$scope.result = 0;
}
};
});
答案 0 :(得分:3)
如果你像这样使用ng-hide和ng-show并且ng-hide评估为false,我的猜测是两个div都应该显示。您是否尝试过只使用一个来确保您的方法正常工作且图像具有正确的值?如果是这样,添加另一个div作为包装并将其用于ng-hide,这样它们就不会互相争斗。