1

I am still fairly new to angular so I was wondering how to best do something like this:

  • I have a json file that returns a variety of items of type 'course' and type 'tutorial'. Tutorials are related to courses with a field

    data = [ { title: foo1, type: course, id:1}, { title: foo2, type: course, id:2}, { title: foo3, type: course, id:3}, { title: t1, type: tutorial, id:4, related_course:2}, { title: t2, type: tutorial, id:5, related_course:2}, { title: t3, type: tutorial, id:6, related_course:3}, ...

In my controller I have functions bound to $scope to allow me to filter by type.

Now in my template

<div ng-repeat="item in data | filter:isCourse">
... display course data
   <div ng-repeat="item in data | filter.isTutorial">
   ... display tutorial data

I would like to find a way to make sure the tutorials displayed match the id of the currently displayed course.

Any suggestions?

Thanks! - V

J99
  • 439
  • 5
  • 12
  • may be help you http://stackoverflow.com/questions/35830338/ng-repeat-filter-to-iterate-only-over-elements-with-certain-property-value/35830473#35830473 – Hadi J Mar 17 '16 at 21:57

1 Answers1

1

You should have then be specific with filters, but do make sure that JSON should be in correct format, as I can see most of the values are not there wrap in "(double quotes)

HTML

<div ng-repeat="course in data | filter: {type: 'course'}">
... display course data
   <div ng-repeat="tut in data | filter: { related_course: course.id }">
   ... display tutorial data

Working Fiddle(Thanks @AWolf)

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Good answer. Here is a [fiddle](https://jsfiddle.net/awolf2904/dos43ny3/) to show how the code is working. – AWolf Mar 17 '16 at 22:07
  • @AWolf thanks for efforts man.. I did update a answer with your fiddle. :-) – Pankaj Parkar Mar 17 '16 at 22:08
  • Thanks @PankajParkar @AWolf - that works! One last thing is is there a simple way to insert a header before the second `ng-repeat` if tutorials are found... ? – J99 Mar 17 '16 at 22:50