0

我刚刚进入 angularJS,对于我的第一个项目,我正在尝试构建一个测验创建者。我希望人们能够创建结果、问题、答案以及结果答案的加权映射。我的 html 看起来像这样:

<div ng-controller="QuizCtrl">

<div class="result" ng-repeat="result in quiz.results">
    <input ng-model="result.title" /> [<a href ng-click="quiz.results.splice($index, 1)">X</a>]<br />
</div>

<div class="question row" ng-repeat="question in quiz.questions">
  <div class="col-sm-4 col-md-4 col-lg-4">
  <input ng-model="question.title" /> [<a href ng-click="quiz.questions.splice($index, 1)">X</a>]<br />
    <a href ng-click="addAnswer($index)">add answer</a>
  </div>
  <div class="col-sm-8 col-md-8 col-lg-8">
    <div class="answer" ng-repeat="answer in question.answers">
      <input ng-model="answer.title" /> [<a href ng-click="question.answers.splice($index, 1)">X</a>]<br />
      <div class="answerresult" ng-repeat="answerresult in answer.answerresults">
        {{ quiz.results[$index].title }}:
        <select ng-change="answerresult.result = $index" ng-model="answerresult.weight" ng-options="i for i in [1,2,3]"></select>
      </div>
    </div>
  </div>
</div>

</div>

底层数据结构如下所示:

angular.module('testApp')
  .controller('QuizCtrl', function ($scope) {
    $scope.quiz = {
        questions: [
            {title: 'question 1', answers: [
                {title: 'answer 1', answerresults:[
                    {'result': 0, weight: 2},
                    {'result': 1, weight: 3}
                ]},
                {title: 'answer 3', answerresults:[
                    {'result': 0, weight: 2},
                    {'result': 1, weight: 2}
                ]}
            ]},
            {title: 'question 2', answers: [
                {title: 'answer 1', answerresults:[
                    {'result': 0, weight: 1},
                    {'result': 1, weight: 2}
                ]},
                {title: 'answer 2', answerresults:[
                    {'result': 0, weight: 2},
                    {'result': 1, weight: 2}
                ]},
                {title: 'answer 3', answerresults:[
                    {'result': 0, weight: 1},
                    {'result': 1, weight: 3}
                ]}
            ]}
        ],
        results: [
          {title: 'result 1'},
          {title: 'result 2'}
        ],
    };
});

现在我的问题:

  1. 每当我向“$scope.quiz.results”添加或删除新项目时,如何更新每个“$scope.quiz.questions[x].answers[y].answerresults”数组?当然嵌套的for循环会做,但这不是很优雅..有没有更好的方法使用角度双向数据绑定?
  2. 如何表示“$scope.quiz.questions[x].answers[y].answerresults[z].result”和“$scope.quiz.results[i]”之间的关系?现在我只是这样设置: ng-change="answerresult.result = $index" 常见做法?或者有没有更好的方法?
  3. 而不是迭代“$scope.quiz.questions[x].answers[y].answerresults”也许它可以迭代“$scope.quiz.results”然后以某种方式为每个答案动态创建 answerresults 数组?

提前感谢您的回答,如果这是一个愚蠢的问题,我们深表歉意。我昨天刚进入角..

4

1 回答 1

0

好的,我现在用嵌套的 for 循环解决了。我不知道这是否是角度的正确方法,但它有效:

$scope.addResult = function() {
  $scope.quiz.results.push({});

  for(var question in $scope.quiz.questions ) {
    for(var answer in $scope.quiz.questions[question].answers) {
        $scope.quiz.questions[question].answers[answer].answerresults.push({});
    }
  }
};

$scope.removeResult = function(index) {
  $scope.quiz.results.splice(index, 1);

  for(var question in $scope.quiz.questions ) {
    for(var answer in $scope.quiz.questions[question].answers) {
        $scope.quiz.questions[question].answers[answer].answerresults.splice(index, 1);
    }
  }
};
于 2015-03-11T08:26:54.137 回答