Javascript AngularJs-如何仅在答案正确时显示答案

Javascript AngularJs-如何仅在答案正确时显示答案,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,首先,我将解释到目前为止我已经实施了什么:- 用户将在单选按钮中选择答案并点击“检查答案”,显示1。第一个答案字段中的文本正确(绿色)或不正确(红色)以及2。“解决方案”(答案)字段(绿色)中的实际正确答案。 我的问题是,它应该只在答案正确时才显示解决方案(答案)字段。现在,即使答案是错误的,解决方案(答案)也会出现 请查看以下链接以获取示例:- 1.如下图所示,一个6米长、重量为20公斤的梯子,其上端位于光滑的墙壁上,下端位于粗糙的水平地面上。确定最小摩擦系数,使梯子不会滑动? 0.14

首先,我将解释到目前为止我已经实施了什么:- 用户将在单选按钮中选择答案并点击“检查答案”,显示1。第一个答案字段中的文本正确(绿色)或不正确(红色)以及2。“解决方案”(答案)字段(绿色)中的实际正确答案。 我的问题是,它应该只在答案正确时才显示解决方案(答案)字段。现在,即使答案是错误的,解决方案(答案)也会出现

请查看以下链接以获取示例:-


1.如下图所示,一个6米长、重量为20公斤的梯子,其上端位于光滑的墙壁上,下端位于粗糙的水平地面上。确定最小摩擦系数,使梯子不会滑动?

0.14 0.29 0.19 0.35 对还是错? 解决方案

考虑梯子的FBD

A=下端的正常反应
B=上端的正常反应
f=作用在水平面上的摩擦力
W=梯子重量=20*10=200 N
考虑水平和垂直平衡
f=B
A=W
所以A=200 N
关于低端的力矩
B*6*sin 60和176;-W*6/2*cos 60和176;=0
B*6*和#8730;3 /2 = 200 * 3 * ½ = 300
B*3*和#8730;3=300
B=100/#8730;3=57.74 N
f=µ;*N=B
µ = 摩擦系数
N=正常反应=A=W=200 N
f=µ;*200=57.74
µ = 57.74/200 = 0.29

重置 核对答案


带示例的解释将非常有用,谢谢。

首先,我要说的是,您没有使用好的做法,因为您在前端存储答案。永远不要存储此类信息,因为即使用户技术水平很低,他/她也会暴露答案和使系统不安全的黑客行为

当选项发生更改时,进行API调用检查并验证答案(如果答案正确),使用ng类添加一个类,如果答案错误,则使用ng类添加一个类,反之亦然


因此,采用这种方法,因为它不会向用户公开您的正确答案,如果您被困在某个地方,请发布问题在于
$scope.answerVisible=true

您需要删除
$scope.answerVisible=true
提交

功能,因为它使您的div在每次单击检查答案按钮功能时可见

所以我所做的,我把它放在了正确的地方

验证
功能中,验证答案,如果答案正确,则将
$scope.answerVisible
设置为true,否则为false

这里更新了功能:

 function validate() {
  allCorrect = true;
  data.forEach(function (val, index) {
  $scope.cb[index].clickable = false;
  $scope.cb[index].showClass = [];
  var groupCheckCorrect = $scope.cb[index].checked.length ? true : false;
  if (Array.isArray(val)) {
    $scope.cb[index].checked.forEach(function (v, i) {
      if (v) {
        if (val.indexOf(i) > -1) {
          $scope.answerVisible =  true;                   
          $scope.cb[index].showClass[i] = 'correct';
        } else {
          $scope.cb[index].showClass[i] = 'wrong';
          allCorrect = false;
          groupCheckCorrect = false;
        }
      }
    });
  } else {
    $scope.cb[index].checked.forEach(function (v, i) {
      if (v) {
        if (val == i) {
          $scope.answerVisible =  true;                 
          $scope.cb[index].showClass[i] = 'correct';
        } else {
          $scope.cb[index].showClass[i] = 'wrong';
          allCorrect = false;
          groupCheckCorrect = false;
        }
      }
    });
  }

  $scope.cb[index].resultClass = groupCheckCorrect ? "all_correct" : "all_wrong";
});

/** for fill up */
$scope.inputDisabled = true;
angular.forEach(fillup_data, function (d, i) {
  if ($scope.fill[i] != undefined && d.toLowerCase() == $scope.fill[i].toLowerCase()) {
    $scope.fillResult[i] = fillup_correct;
  } else {
    $scope.fillResult[i] = fillup_wrong;
  }
});
user_fill_answer = [];
user_fillResult_answer = [];
angular.copy($scope.fill, user_fill_answer);
angular.copy($scope.fillResult, user_fillResult_answer);
/** */

}

感谢您的快速回复。实际上,我是为Epub创建的,Epub基本上是一种扩展名为.Epub的电子书文件格式,可以在智能手机、平板电脑、电脑或电子阅读器等设备上下载和阅读。。因此,当我们将html转换为epub时,所有内容都将被加密。这个问题的解决方案会很有帮助。我一定会给我一些时间,我会推迟!!继续问!!除了你提供的主要代码之外,告诉我们你迄今为止尝试了什么,通过将其编辑到你的问题中而不是在评论中来解决它!。。。它被称为“显示努力”,是对付失败选民的良药。
 function validate() {
  allCorrect = true;
  data.forEach(function (val, index) {
  $scope.cb[index].clickable = false;
  $scope.cb[index].showClass = [];
  var groupCheckCorrect = $scope.cb[index].checked.length ? true : false;
  if (Array.isArray(val)) {
    $scope.cb[index].checked.forEach(function (v, i) {
      if (v) {
        if (val.indexOf(i) > -1) {
          $scope.answerVisible =  true;                   
          $scope.cb[index].showClass[i] = 'correct';
        } else {
          $scope.cb[index].showClass[i] = 'wrong';
          allCorrect = false;
          groupCheckCorrect = false;
        }
      }
    });
  } else {
    $scope.cb[index].checked.forEach(function (v, i) {
      if (v) {
        if (val == i) {
          $scope.answerVisible =  true;                 
          $scope.cb[index].showClass[i] = 'correct';
        } else {
          $scope.cb[index].showClass[i] = 'wrong';
          allCorrect = false;
          groupCheckCorrect = false;
        }
      }
    });
  }

  $scope.cb[index].resultClass = groupCheckCorrect ? "all_correct" : "all_wrong";
});

/** for fill up */
$scope.inputDisabled = true;
angular.forEach(fillup_data, function (d, i) {
  if ($scope.fill[i] != undefined && d.toLowerCase() == $scope.fill[i].toLowerCase()) {
    $scope.fillResult[i] = fillup_correct;
  } else {
    $scope.fillResult[i] = fillup_wrong;
  }
});
user_fill_answer = [];
user_fillResult_answer = [];
angular.copy($scope.fill, user_fill_answer);
angular.copy($scope.fillResult, user_fillResult_answer);
/** */