0

I have a data structure (sample provided below) where I need to iterate upon this set of questions and be able to template them in angular. Each "question" has this structure:

{
    id: 2, // unique id
    label: 'some label', // any label
    type: 'input', // the type of html element
    options: [], // other questions are nested in here (recursive)
    children: [] // other questions are nested in here (recursive)
}

As mentioned above, the type describes the type of html element and the difference between options and children is that the set of questions that rendered in options are display in the same line as the parent question whereas the questions inside of children are rendered one line below with a little bit of left margin.

I'm not familiar enough with Angular on how you'd achieve this, given that the basic ng-repeat would no suffice here (from what I understand) and I'm curious how one would go about templating this sort of data structure.

Many thanks, in advance, for the help!

Here's a sample data structure:

[
    {
        id: 1,
        label: 'something',
        type: 'text',
        options: [
            {
                id: 2,
                label: 'another',
                type: 'text',
                options: [],
                children: []
            }
        ],
        children: [
            {
                id: 83,
                label: 'cool',
                type: 'input',
                options: [],
                children: []
            },
            {
                id: 121,
                label: 'another cool thing',
                type: 'label',
                options: [
                    {
                        id: 451,
                        label: 'only a label',
                        type: '',
                        options: [],
                        children: [
                            {
                                id: 901,
                                label: 'a cool info',
                                type: 'label',
                                options: [],
                                children: []
                            }
                        ]
                    }
                ],
                children: []
            }
        ]
    },
    {
        id: 27,
        label: 'some text',
        type: 'label',
        options: [
            {
                id: 541,
                label: 'hey there',
                type: 'label',
                options: [],
                children: []
            }
        ],
        children: [
            {
                id: 761,
                label: 'hi there',
                type: 'checkbox',
                options: [],
                children: []
            },
            {
                id: 165,
                label: 'cool',
                type: 'text',
                options: [
                    {
                        id: 1090,
                        label: 'neat',
                        type: 'input',
                        options: [],
                        children: [
                            {
                                id: 9891,
                                label: 'tell me',
                                type: 'textarea',
                                options: [],
                                children: []
                            }
                        ]
                    }
                ],
                children: []
            }
        ]
    }
];
2
  • You'd need to use nested ng-repeats and apply ng-class depending on the kind of item you are looping trough to make options and children look different applying css classes. Commented Feb 16, 2017 at 23:44
  • Yes, that's what I originally had, however, it doesn't solve the following issues: --------- 1) nested ng-repeats is only as good as many the levels you write. hence why I was wondering if there's any other way of doing this ------- 2) ng-class doesn't account for the different types of html elements. i was thinking of using ng-switch and having all the various ones there, but is there any other way? Commented Feb 16, 2017 at 23:56

1 Answer 1

1

I'd suggest using ng-if instead ng-class so you can call an external html template on the fly. You could event tune that template to have inside another ng-repeat if options or children exists so you loop till the end of the array/object no matter it's depth.

Sign up to request clarification or add additional context in comments.

1 Comment

Do you have any examples by chance?

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.