【问题标题】:nested ng-repeat, radio button validate嵌套 ng-repeat,单选按钮验证
【发布时间】:2016-06-03 06:28:39
【问题描述】:

我有两个嵌套的 ng-repeat 生成多个问题,每个问题都包含一堆单选按钮。当没有为某个问题选择单选时,我无法想出一种有效的方法来显示验证错误消息,并在用户点击选择时获取所有选定的选项

    <ul>
      <li ng-repeat="question in questionModel.questionaire">{{question.question}}
      <span style = "color:red" ng-show = "!!!ANOTHER CONDITION HERE!!! && questionModel.submitClicked != false">Please choose an answer </span>

          <ul>

            <li ng-repeat="answer in question.answers">
              <input  type="radio"
                      ng-click="questionModel.handleChange(answer)"
                      value={{answer}}
                      name="radio_{{$parent.$index}}"
                      required>
              {{answer}}
            </li>
          </ul>

      </li>
    </ul>

你建议我如何处理这个问题?在ul 上使用getElementById 然后检查所有子节点?听起来确实很乱,但很容易,我可以轻松获得所有被选中的按钮。有没有更好的方法来处理验证错误消息?

如果所有单选按钮都在同一个问题下很容易,我可以在单选按钮上使用 ng-model。

编辑:添加信息

控制器:

  myApp.controller('questionController', function($scope, $http, toServer){

    $scope.questionModel = {
      questionaire: [],
      submitClicked: false,

      handleChange: function(ans){
        console.log(ans);
      },

      submit: function(){
        this.submitClicked = true;
        console.log(JSON.stringify(this.questionaire));
      }
    }

    toServer.get_All_Questions(function(response){
      $scope.questionModel.questionaire = response;
    });

  })

我为 ajax 调用创建的服务:

//this service handle all the ajax call to server
myApp.factory('toServer', function($http){

  return {

    get_All_Questions: function(callback){
      var url = "/health1/server/questions";

      $http.get(url).success(function(response){
        response.forEach(function(item){
          item.answers = (item.answers != null) ? item.answers.split(",") : [];
          item.answered = false;
        });
        callback(response);
      }).error(function(response){
        console.log("error " + response)
      });
    }

  }

});

$scope.questionModel.questionaire(来自服务器的响应)的结构是一个 json 数组:

[{id: "1", question: "what's your name?", answers: ["tom", "mary"]},
{id: "2", question: "what's your age?", answers: [1, 2]}
]

【问题讨论】:

  • !!question.answered ?
  • o 那只是一个变量,我正在玩弄它希望能想出一个解决方案
  • @LoremIpsum questionModel.handleChange() 做什么?
  • 另一个毫无意义的功能。它所做的只是打印出使用console.log 传递给handlechange 的参数

标签: angularjs


【解决方案1】:

没有属性可以识别问题是否已被回答。因此,我努力创建了isAnswered = true/false 这样的属性,并将其默认初始化为 false。

angular.forEach($scope.questionModel.questionaire, function(ques) {
    ques.isAnswered = false;
    return ques;
});

When an answer is selected, the function handleChange sets isAnswered = true.

handleChange: function(ans, ques){
    ques.isAnswered = true;
    console.log(ques);
},

最后,在提交点击时,条件检查提交是否被点击,如果有任何问题没有回答,它将显示以下消息:

<span style = "color:red" 
   ng-show = "questionModel.submitClicked && !question.isAnswered">
   Please choose an answer 
</span>

Working Plunker

【讨论】:

    【解决方案2】:

    这很简单 - 使用 ng-model 作为您的单选按钮:

    <ul>
      <li ng-repeat="question in questionModel.questionaire" {{question.question}}
      <span style = "color:red" ng-show="!question.selectedAnswer && questionModel.submitClicked">Please choose an answer</span>
          <ul>
            <li ng-repeat="answer in question.answers">
              <input  type="radio"
                      ng-model="question.selectedAnswer"
                      ng-click="questionModel.handleChange(answer)"
                      value={{answer}}
                      name="radio_{{$parent.$index}}">
              {{answer}}
            </li>
          </ul>
      </li>
    </ul>
    

    【讨论】:

    • 嘿,是的,ng-model 给我带来了问题。现在,当我单击问题 1 中的单选按钮,然后单击问题 2 中的单选按钮时,不同问题中的所有单选按钮都“链接”,问题 1 的单选按钮选择被清除。这很奇怪,因为它们的名字不同
    • @user308553 向我展示questionModel.questionaire 的生成方式以及前端的外观。所以我可以想象是什么问题。
    • 我在原帖中添加了一些额外的代码,谢谢
    • 我还添加了一个从服务器接收到的数据结构示例
    • @user308553 谢谢,现在看看
    猜你喜欢
    • 2016-12-03
    • 2015-05-07
    • 1970-01-01
    • 2016-04-22
    • 2022-11-11
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多