首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于其他数据结构的angularJS数据结构更新

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

Stack Overflow用户
提问于 2015-03-11 06:24:51
回答 1查看 134关注 0票数 0

我刚进入angularJS,在我的第一个项目中,我试图构建一个测试创建者。我希望人们能够创造出结果,问题,答案和结果的加权映射。我的html看起来是这样:

代码语言:javascript
复制
<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>

底层的数据结构如下所示:

代码语言:javascript
复制
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.questionsx.answersy.answerresults”“添加或删除一个新项时,如何更新每个$scope.quiz.results数组?当然,嵌套的for-循环可以,但这并不是很优雅。有更好的方法使用角度双向数据绑定吗?
  2. 我如何表示"$scope.quiz.questionsx.answersy.answerresultsz.result“与"$scope.quiz.resultsi”之间的关系?现在,我刚刚设置了以下内容: ng-change="answerresult.result = $index“通用实践?还是有更好的方法?
  3. 而不是在"$scope.quiz.questionsx.answersy.answerresults“上迭代,也许它可以在"$scope.quiz.results”上迭代,然后以某种方式动态地为每个答案创建应答结果数组?

提前感谢你的回答,如果这是个愚蠢的问题,很抱歉。我昨天突然陷入困境..。

EN

回答 1

Stack Overflow用户

发布于 2015-03-11 08:26:54

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

代码语言:javascript
复制
$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);
    }
  }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28979929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档