在Angular.js中显示json数据

时间:2016-03-31 11:12:00

标签: angularjs json

我使用下面的代码,我只得到问题ID,但我需要的是当我点击Save按钮时我要显示questions.idoption.id和{{1}文本框下方的{} selectedtrue。下面的代码是正确的,但想要像false一样。

array

controllers.js

$scope.save = function() { $scope.msg = 'Question IDs:'; $scope.questions.forEach(function(el){ console.log(el) $scope.msg += el.Id + ','; }) } 中存储所有json数据。

$scope.questions

home.html

<div ng-repeat="question in filteredQuestions"> <div class="label label-warning">Question {{currentPage}} of {{totalItems}}.</div> <div class="row"> <div class="row"> <h3>{{currentPage}}. <span ng-bind-html="question.Name"></span></h3> </div> </div> <div class="row text-left options"> <div class="col-md-6" ng-repeat="option in question.Options" style="float:right;"> <div class="option"> <label class="" for="{{option.Id}}"> <h4> <input id="{{option.Id}}" type="checkbox" ng-model="option.Selected" ng-change="onSelect(question, option);" /> {{option.Name}}</h4> </label> </div> </div> </div> <div class="center"><button ng-click="save()">Save</button></div> <div class="center"><textarea rows="5" cols="50">{{msg}}</textarea></div> </div>

csharp.js

2 个答案:

答案 0 :(得分:0)

您可以将整个对象传递给您的函数:

<div ng-repeat="question in filteredQuestions">
        <div class="label label-warning">Question {{currentPage}} of {{totalItems}}.</div>
        <div class="row">
            <div class="row">
                <h3>{{currentPage}}. <span ng-bind-html="question.Name"></span></h3>
            </div>
        </div>
        <div class="row text-left options">
            <div class="col-md-6" ng-repeat="option in question.Options" style="float:right;">
                <div class="option">
                    <label class="" for="{{option.Id}}">
                       <h4> <input id="{{option.Id}}" type="checkbox" ng-model="option.Selected" ng-change="onSelect(question, option);" />
                        {{option.Name}}</h4>
                    </label>
                </div>
            </div>
            </div>

        <div class="center"><button ng-click="save(question, option)">Save</button></div>
        <div class="center"><textarea  rows="5" cols="50">{{msg}}</textarea></div>
    </div>

使用以下命令在javascript中记录它们:

 $scope.save = function(question, option) {
    $scope.msg = 'Question IDs:';
    console.log(question + ':' + option) 
 }

答案 1 :(得分:0)

在控制器中使用此代码,使问题ID为所选选项ID和未选择的选项ID。

$scope.save = function() {
  console.log('Question IDs:' + question.id);
  $scope.questions.Options.forEach(function(option){
        if(option.selected){
           console.log('Selected Options:' +option.id)
           $scope.msg = 'Question IDs: ' + question.id + ', Selected option id: ' + option.id;
        }
        else{
           console.log('Not selected Options:' +option.id)
        }
  })
}

希望有所帮助