【问题标题】:how to push in array which is inside of services and push in object of an object如何推入服务内部的数组并推入对象的对象
【发布时间】:2014-06-09 13:36:41
【问题描述】:
 <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:真\假(复选框) 2:多个答案(复选框) 3. 单一答案(单选) 这该怎么做 ??虽然我在测验中添加了一个选项,但不知道如何执行此操作并创建其布局??

【问题讨论】:

  • 你可以为它制作一个小提琴吗?

标签: javascript angularjs html twitter-bootstrap css


【解决方案1】:

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

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

【讨论】:

  • @MohammadFaizankhan:谢谢你的建议!我会……等等……在我回答之前我确实读过这个问题(我很傻,但我做到了)。也许你想分享一个更丰富的评论......
猜你喜欢
  • 2015-11-14
  • 1970-01-01
  • 2014-08-10
  • 2016-05-09
  • 2017-12-05
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
相关资源
最近更新 更多