I have a rootScope
variable that will contain category for products, and each category may or may not have children. This is how I assign the rootScope
:
$rootScope.categoryList = $http.get('category').then((result) -> result.data)
This will make request (to Laravel route) to get me arrays of category data, the design of the data looks something like this:
$data = array(
{category: Category, children: array('child1, child2, child3')},
{category: Category2, children: null},
{category: Category3, children: array('anotherchild1, anotherchild2')}
);
And the way i generate the array is this:
public static function GetCategoryListings($ignore=false) {
$results = DB::table('category')->where('parent','-')->get();
$data = array();
if(!is_null($results)) {
$i = 0;
foreach($results as $result) {
$data[$i]['category'] = $result;
$child_result = DB::table('category')->where('parent',$result)->get();
if(!is_null($child_result)) {
foreach($child_result as $child) {
$data[$i]['children'][] = $child;
}
}
else
$data[$i]['children'] = null;
$i++;
}
}
return $data;
}
Now, I would like to print my output into the view in Angular, how will I do that? I did something like this:
<li ng-repeat="cat in categoryList">
{{cat.category}}
</li>
But it's not working, plus I couldn't output the children. Is there any way around this?
EDIT
Solved my problem by changing to something like this in my view:
<ul>
<li ng-repeat="cat in categoryList">
{{cat.category}}
<ul ng-if="cat.children != null">
<li ng-repeat="ch in cat.children">{{ch}}</li>
</ul>
</li>
</ul>