如何推入服务内部的数组并推入对象的对象

时间:2014-06-09 13:36:42

标签: javascript angularjs html5 twitter-bootstrap css3

 <pre lang="HTML">
 <ul>
 <li data-ng-repeat="q in QuizObj">
 <fieldset><legend>All Quizes </legend>
     <h1>{{q.Quiz  }}</h1>
     <h3>options</h3>
     <h3>answer</h3>
        &lt;input type="radio" />{{q.options[1]}}
        &lt;input type="radio" />{{q.options[2]}}
        &lt;input type="radio" />{{q.options[3]}}
        &lt;input type="radio" />{{q.options[4]}}
        <h4>Answer</h4>
        &lt;input type="radio" />{{q.answer}}
  </fieldset>
        </li>
    </ul>
 &lt;input type="text" ng-model="ans">
    &lt;input type="text" ng-model="Question" >

    &lt;input type="text" ng-model="type">
    &lt;input type="button" data-ng-click="addQuiz()">

,这是js角度代码

angular.module('quizApp',[])
.controller('mainCtrl',function($scope,crudService){

$scope.name="this";
        alert(JSON.stringify(crudService.quiz));
        $scope.QuizObj= crudService.quiz;
    })
    .service('crudService',function(){
        this.quiz=[
            {
                Quiz: "what is the C#",
                options: {  1: 'P-lang',
                            2:'h-lang',
                            3: 'A-lang',
                            4: 'nothing'
                        },
                type: 'radio',
                answer: 1
            },
            {
                Quiz: "what is the J#",
                options: {  1: 'P-lang',
                    2:'h-lang',
                    3: 'A-lang',
                    4: 'nothing'
                },
                type: 'radio',
                answer: 1
            },
            {
                Quiz: "what is the VB",
                options: {  1: 'PL',
                    2:'hL',
                    3: 'AL',
                    4: 'nothing'
                },
                type: 'radio',
                answer: 1
            }
        ]

    })
我有一些问题 我想在测验中添加问题,而测验对象在服务中如何添加这个我正在做的事情 此

   $scope.addQuiz= function($scope){
                crudService.quiz.push({
                    Quiz: $scope.Question,
                    type: $scope.type,
                    answer: $scope.ans
                })

        }

但显示错误 TypeError:无法读取未定义的属性“问题” 如何推进服务的对象(测验)。 ?? 如何在Quiz对象中推入另一个对象的选项。 ?? 加上我想让用户添加答案类型 类型是 1:true \ false(复选框) 2:Mulitiple回答(复选框) 单一答案(电台) 这该怎么做 ??虽然我在测验中添加了一个选项,但不知道如何做到这一点并创建它的布局??

1 个答案:

答案 0 :(得分:1)

在调用$scope函数时,您显然没有传递addQuiz()参数。由于$scope位于addQuiz函数的JS上下文中,因此您可以完全省略它:

$scope.addQuiz = function () {
    crudService.quiz.push({
        Quiz: $scope.Question,
        type: $scope.type,
        answer: $scope.ans
    });
};