在多个记录和条件的基础上设置输入类型文本的ng-model

时间:2016-07-18 06:06:17

标签: angularjs typescript

<!-- locally referred ExtJS library files

因此,如果答案列表中的答案为<!-- locally referred ExtJS library files --> ,我希望使用json = [ question-list:{ question-Text: "the car more than 25 years old", answer-list:[ {answerDisplayText: "58",answered:true}, {answerDisplayText: "", answered:false}] } ] 使用"answerDisplayText"填充输入文本框,否则将为空。

3 个答案:

答案 0 :(得分:0)

  

如果在答案列表中回答为真,则其他为空白。

根据您的输入数据:

{answerDisplayText: "58",answered:true},         
{answerDisplayText: "", answered:false}

如果您只是绑定到answerDisplayText,它应该可以正常运行:)

答案 1 :(得分:0)

使用ng-if并检查给定问题是否没有答案。如果没有显示单独的div,则输入为空。见下面的例子。

angular
  .module('exampleApp', [])
  .controller('ExampleController', ExampleController);

function ExampleController() {
  var vm = this;
  vm.jsonObject = [{
    questionText: "the car more than 25 years old",
    answerList: [{
      answerDisplayText: "58",
      answered: true
    }, {
      answerDisplayText: "",
      answered: false
    }]
  }, {
    questionText: "this question has none answered",
    answerList: [{
      answerDisplayText: "58",
      answered: false
    }, {
      answerDisplayText: "",
      answered: false
    }]
  }];
  vm.isHavingAnswer = function(answerList) {
    var isHavingAnswer = false;
    angular.forEach(answerList, function(answer) {
      if (answer.answered) {
        isHavingAnswer = true;
      }
    });
    return isHavingAnswer;
  }
}
<!DOCTYPE html>
<html ng-app='exampleApp'>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
</head>

<body ng-controller="ExampleController as vm">
  <div ng-repeat="questionList in vm.jsonObject">
    <p>Question: {{questionList.questionText}}</p>
    <div ng-if="answer.answered " ng-repeat="answer in questionList.answerList">
      <label>
        Answer:
        <input type="text" ng-model="answer.answerDisplayText">
      </label>
    </div>
    <div ng-if="!vm.isHavingAnswer(questionList.answerList)">
      <label>
        Answer:
        <input type="text" ng-model="questionList.answerList.newAnswer">
      </label>
    </div>
  </div>
</body>

</html>

答案 2 :(得分:0)

<div ng-hide="json.answered==false"><input type="text" ng-model="json.answerDisplayText"></div><div ng-hide="json.answered==true"><input type="text"></div>