AngularJS:在ng-reeat中从php输出数组(带键)


AngularJS: output array (with keys) from php in ng-repeat?

我有一个rootScope变量,它将包含产品的类别,每个类别可能有也可能没有子类别。这就是我分配rootScope:的方式

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

这将请求(到Laravel路由)获取类别数据的数组,数据的设计看起来像这样:

$data = array(
        {category: Category, children: array('child1, child2, child3')},
        {category: Category2, children: null},
        {category: Category3, children: array('anotherchild1, anotherchild2')}
    );

我生成数组的方式是:

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;
}

现在,我想将我的输出打印到Angular中的视图中,我将如何做到这一点?我做到了像这样的东西:

<li ng-repeat="cat in categoryList">
                {{cat.category}}
            </li>

但它不起作用,加上我不能输出孩子们。有办法解决这个问题吗?

编辑

在我看来,通过改成这样解决了我的问题:

<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>

这里可能有很多不同的问题,但很可能就是这样。

Angular 1.2不再自动打开模板中的承诺。您必须将语法更改为以下内容:

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

请记住,如果你依赖categoryList作为承诺,这可能会破坏你应用程序中的其他内容。