我正在制作一个平均堆栈应用程序,但遇到了问题! 在第10个问题被提出后,我试图隐藏所有内容。 我使用指令ng-hide,当我到达第10个问题时,它被设置为true!这是代码:
<body>
<div ng-hide="quizend" class="ng-hide">
<div class="container" ng-controller="questionController">
<button ng-hide="quizzstarted" class="btn btn-warning btn-lg" ng-click="startQuizz()">Start Quizz</button>
<form ng-show="quizzstarted">
<h1>{{currentQuestion}}</h1>
<div>{{question}}</div>
<button class="btn btn-success btn-lg" ng-click="clickYes()">JA</buztton>
<button class="btn btn-danger btn-lg" ng-click="clickNo()">NEE</button>
<p ng-show="visibility">
<button class="btn btn-information btn-lg" ng-click="nextAnswer(1)">1</button>
<button class="btn btn-information btn-lg" ng-click="nextAnswer(2)">2</button>
<button class="btn btn-information btn-lg" ng-click="nextAnswer(3)">3</button>
<button class="btn btn-information btn-lg" ng-click="nextAnswer(4)">4</button>
<button class="btn btn-information btn-lg" ng-click="nextAnswer(5)">5</button>
</p>
</form>
</div>
</div>
现在是js控制器代码:
angular.module('app').controller('questionController', ['$scope', '$http','$location', function ($scope, $http, $location){
$scope.currentQuestion = '';
$scope.quizzstarted = false;
$scope.isnextStep = false;
$scope.message= "it works";
$scope.startQuizz = function (){
$scope.quizzstarted = true;
$scope.quizend=false;
$scope.getQuestionByID(1);
}
$scope.getQuestionByID = function (id){
$http.get('/api/questions/' + id).success(function(data) {
$scope.currentQuestion = data.id;
questionid = data.id;
$scope.question = data.question;
});
}
$scope.clickYes = function() {
if ($scope.visibility==false || $scope.visibility==null)
{
$scope.visibility=true;
}
else if ($scope.visibility==true)
{
$scope.visibility=false;
}
}
$scope.clickNo = function(){
var answer = {
qid : $scope.currentQuestion,
answer : "No",
nextanswer:""
};
checkEnding();
sendAnswer(answer);
}
$scope.nextAnswer = function(weight){
var answer = {
qid : $scope.currentQuestion,
answer : "Yes",
nextanswer: weight
};
checkEnding();
sendAnswer(answer);
$scope.visibility = false;
}
var sendAnswer = function (answer){
$http.post('/api/questions', answer).success(function(data){
$scope.currentQuestion++;
$scope.getQuestionByID($scope.currentQuestion);
});
}
var checkEnding = function () {
if($scope.currentQuestion<=10)
{
alert($scope.currentQuestion)
}
else
{
$scope.quizend=true;
}
}
}]);
答案 0 :(得分:2)
我认为这是因为你没有改变你的$ scope.quizzstarted。 编辑你的checkending函数:
var checkEnding = function () {
if($scope.currentQuestion<=10) {
alert($scope.currentQuestion)
}
else {
$scope.quizend=true;
$scope.quizzstarted = false;
}
}