[英]Angular ng-show not working on initial page load
我正在使用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;
}
};
});
如果您像這樣一起使用ng-hide和ng-show,而ng-hide的計算結果為false,我猜這兩個div都應該顯示。 您是否嘗試過僅使用一種方法來確保您的方法正常工作並且圖像具有正確的價值? 如果是這樣,請添加另一個div作為包裝器,並將其用於ng-hide,這樣它們就不會互相競爭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.