Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于其他数据结构更新angularJS中的数据结构_Javascript_Angularjs_Data Structures_2 Way Object Databinding - Fatal编程技术网

Javascript 基于其他数据结构更新angularJS中的数据结构

Javascript 基于其他数据结构更新angularJS中的数据结构,javascript,angularjs,data-structures,2-way-object-databinding,Javascript,Angularjs,Data Structures,2 Way Object Databinding,我刚进入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.

我刚进入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'}
        ],
    };
});
现在我的问题是:

  • 每当我向“$scope.quick.results”添加或删除新项目时,如何更新每个“$scope.quick.questions[x].answerresults”数组?当然,嵌套for循环也可以,但这并不十分优雅。。有没有更好的方法使用角度双向数据绑定
  • 如何表示“$scope.quick.questions[x].答案[y].回答结果[z].结果”和“$scope.quick.results[I]”之间的关系?现在我刚刚设置了这个:ng change=“answerresult.result=$index”常规做法?还是有更好的办法
  • 与其迭代“$scope.quick.questions[x].答案[y].answerresults”,不如迭代“$scope.quick.results”,然后为每个答案动态创建answerresults数组

  • 提前感谢您的回答,如果这是一个愚蠢的问题,请道歉。我昨天刚跳进angular…

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

    $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);
        }
      }
    };