0

I have an array of sections where a section is pushed into the array when the user clicks a checkbox. It is defined like this:

$scope.print = {
  sections:[] 
};

Once I have all of the selected Sections, I use a service to get a list of questions for each selected section like this:

var questions = [];
myService.getQuestions(id)
  .success(function (data) {
    questions = data;
  });

Then I assign the returned questions to the original array like this:

angular.forEach($scope.print.sections,
    function (value, key) {
        if (value.QuestionSectionID === id) {
            $scope.print.sections[key].questions = questions;
        }
    });

The assignment "seems" to work, but I am unable to access the particular fields in the questions array by their field names.

On my HTML page, I have this:

<div ng-repeat="ps in print.sections">
    <div>
        <h4>{{ps.vchSectionName}}</h4>
    </div>
    <div ng-repeat="section in ps.questions">
        <div ng-repeat="q in section">
            {{q.vchQuestionText}}
        </div>
    </div>
</div>

When I try to access the "q.vchQuestionText" field, my HTML is blank, however if I simple do the following:

<div ng-repeat="q in section">
  {{q}}
</div>

I can then see ALL of the information contained in each field of "q". But I need to access each field in "q" by it's name. What am I missing here?

Any assistance is greatly appreciated!

7
  • Can you give some sample data? Commented Jan 9, 2017 at 18:45
  • What exactly do you wish to see? $scope.print.sections is an array of sections. Each Section has an id, and a name. The questions array contains about 30 fields. Commented Jan 9, 2017 at 18:49
  • In js you have sections array with nested Questions array, but in html you use ngrepeat for Questions first and name each section Commented Jan 9, 2017 at 18:52
  • I'm not sure what you mean. The first ng-repeat is "ps in print.sections", the second one "section in ps.questions" gets the questions. Commented Jan 9, 2017 at 18:55
  • A section obviously contains more than an id and a name. It contains QuestionSectionID and vchSectionName. Maybe that's what you mean by "id and a name", but asking volunteers to guess at your data model will lead to confusion. Also, naming is important; you have section in ps.questions... why is that not question in ps.questions, since ps appears to be a section itself? Commented Jan 9, 2017 at 18:59

1 Answer 1

0

Okay, the reason I could display all the data in "q", but could not specify a field is because I had one too many ng-repeat directives.

All I needed was the following:

<div ng-repeat="ps in print.sections">
<div>
    <h4>{{ps.vchSectionName}}</h4>
</div>
    <div ng-repeat="q in ps.questions">
        {{q.vchQuestionText}}
    </div>
 </div>

Hopefully, this will help someone else. Thank you all for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.