控制器已经在init上加载了$ scope.shops。与延期和承诺等异步。我为每个可用的商店创建一个面板。然后,我想为每个项目添加列。
我在控制器getItemsPerShop(item)中也有一个方法,该方法也是异步的。.我目前有两个面板(有两个商店),但是项目相同。.可能是异步问题和$ scope.items的原因下载得足够快,我将如何解决这个问题。
<div ng-repeat="shop in shops">
<div class="panel panel-default">
<div class="panel-heading">shop {{shop}}</div>
<table class="table">
<thead ng-init="getItemsPershop(shop)">
<tr>
<th>Number</th>
<th>Date</th>
</tr>
</thead>
<tbody ng-repeat="item in items">
<tr>
<th scope="row">{{item.nr}}</th>
<td>{{item.desc}}</td>
</tr>
</tbody>
</table>
</div>
</div>
这就像一个嵌套的情况,其中每个面板都需要加载其行。
模板的外观,您items
的作用域中只有一个数组。但是,您需要将其嵌套在商店中,否则无法区分它们。
最后,它看起来像这样:
<tbody ng-repeat="item in shop.items">
<tr>
<th scope="row">{{item.nr}}</th>
<td>{{item.desc}}</td>
</tr>
</tbody>
如另一个答案中所指出的,您可以将项目分配给items
当前商店本地范围内的字段。但是,我不鼓励您这样做。从文档:
ngInit只有少数适当的用法,例如别名ngRepeat的特殊属性,如下面的演示所示;以及通过服务器端脚本注入数据。除了这几种情况,您应该使用控制器而不是ngInit来初始化作用域上的值。
在angular中,您应该在控制器中初始化模型并通过模板进行渲染。混合使用这两种方法会使您的代码更难以理解,测试和维护。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句