简体   繁体   English

ng-repeat与ng-init结合使用

[英]ng-repeat in combination with ng-init

Controllers loads $scope.shops on init already. 控制器已经在init上加载了$ scope.shops。 Async with defer and promise etc.. I create a panel for each shop there is available. 与延迟和承诺等异步。我为每个商店创建一个可用的面板。 Then I'd like to add columns for each item. 然后我想为每个项目添加列。

I have a method in controller getItemsPerShop(item) which is also async etc.. I currently get two panels (there are two shops), but the items are the same.. probably cause of the async issue and the $scope.items not getting downloaded fast enough, how would I go about solving this. 我在控制器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>

It's like a nested situation where the each panel need to load it's rows. 这就像一个嵌套的情况,每个面板需要加载它的行。

I will go for making getItemsPershop() in controller. 我将在控制器中创建getItemsPershop()

Did you forget the items = in ng-init ? 你忘记了ng-inititems =吗?

<div ng-repeat="shop in shops">
        <div class="panel panel-default">
            <div class="panel-heading">shop {{shop}}</div>
            <table class="table">
                <!-- forgot "items =" ? -->
                <thead ng-init="items = 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>

The way your template looks, you have one array items in your scope. 模板的外观,您的范围中有一个数组items However, you need to nest it in the shop, otherwise you can't distinguish them. 但是,您需要将它嵌套在商店中,否则您无法区分它们。

In the end it will look like this: 最后它看起来像这样:

<tbody ng-repeat="item in shop.items"> 
    <tr>
        <th scope="row">{{item.nr}}</th>
        <td>{{item.desc}}</td>
    </tr>
</tbody>

As pointed out in another answer you can assign the items to a field items in a scope local to the current shop. 如在另一个答案中所指出的,您可以将项目分配给当前商店本地范围内的字段items However I would discourage you from doing so. 但是我不鼓励你这样做。 From the docs : 来自文档

There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; ngInit只有少数适​​当的用途,例如用于别名ngRepeat的特殊属性,如下面的演示所示; and for injecting data via server side scripting. 并通过服务器端脚本注入数据。 Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope. 除了这几种情况,您应该使用控制器而不是ngInit来初始化作用域上的值。

In angular you are supposed to init a model in your controller and render it via a template. 在角度中,您应该在控制器中初始化模型并通过模板渲染它。 Mixing these two makes your code harder to understand, test, and maintain. 混合这两者会使您的代码更难理解,测试和维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM