Search code examples
javascriptangularjsangularjs-ng-repeatangular-ng-if

ng-repeat and ng-if syntax for arrays within arrays


I have an array within an array and I'm having some issues with ng-repeats and ng-ifs. My array looks something like this:

data.stage = [];
task_obj = {};

data.stage.push({
                workflow_stage: wf.getDisplayValue('name'),
                task: []
            });

task_obj.number = userTasks.getValue('number');
            task_obj.parent = userTasks.getValue('parent');
            task_obj.state = userTasks.getValue('state');
            task_obj.url = userTasks.getDisplayValue('url');
            task_obj.incompleteCounter = incompleteCounter;
            task_obj.icon = icon;
            task_obj.status = status;
            task_obj.style = style;
            task_obj.short_description = userTasks.getValue('short_description');
            task_obj.bundle_name = bundle.getValue('bundle_name');
            task_obj.workflow = bundle.getDisplayValue('workflow');
            task_obj.workflow_stage = bundle.getDisplayValue('workflow_stage');

for(var a=0; a < data.stage.length; a++){
                if(bundle.getDisplayValue('workflow_stage') == data.stage[a].workflow_stage) {
                    data.stage[a].task.push(task_obj);
                }
            }
        }   

If I have an ng-repeat that looks like ng-repeat="item in data.stage track by $index", how would I access the short_description for example? Would it be like {{item.task.short_description}}?

Similarly, if I wanted to write an ng-if where bundle_name is "MyBundle", how would I write it? I've tried ng-if="item.task.bundle_name=='MyBundle'", but it obviously doesn't work.

Can anyone guide me on the correct syntax?


Solution

  • You need to use ng-repeat for task also

    <div ng-repeat=ng-repeat="item in data.stage track by $index">
        <div ng-repeat=ng-repeat="task in item.task track by $index">
             <div ng-if="task.bundle_name == 'MyBundle' ">
                 My bundle condition 
             </div>
             <span>{{task.short_description}}</span>
        </div>
    </div>