0

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>

1 Answer 1

0

There could be a lot of different issues at play here, but this is most likely it.

Angular 1.2 no longer automatically unwraps promises in the templates. You will have to change your syntax to the following:

$http.get('category').then((result) -> $rootScope.categoryList = result.data)

Keep in mind that this might break some other stuff in your app if you relied on categoryList being a promise.

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.