简体   繁体   English

将ng-init与ng-repeat一起使用时会覆盖

[英]overwite when use ng-init with ng-repeat

I use the following loadLocation(asset.id) function to load location for specific asset with ng-repeat and ng-init ,but I get latest location.city and location.state for all rows 我使用以下loadLocation(asset.id)函数通过ng-repeat和ng-init加载特定资产的位置,但是我获得了所有行的最新location.city和location.state

   <tr ng-repeat="asset in assets">
        <td>{{asset.name}}</td>
        <td>{{asset.obj_type.name}}</td>
        <td ng-init="loadLocation(asset.id)">
             {{location.city}}
        </td>
        <td>{{location.state}}</td>
   </tr>

Angular controller 角度控制器

    $scope.loadLocation= function(assetId) {
     Location.locationByIsprimary({assetId: assetId}, function(result) {
                $scope.location=result;
            });
        };

Can anyone help me please? 谁能帮我吗?

You are creating location inside a scope only once, you need to create it for each element inside ng-repeat array, so that will reflect on html. 您只能在scope内创建location一次,您需要为ng-repeat数组中的每个元素创建location ,以便将其反映在html上。

Markup 标记

<tr ng-repeat="asset in assets">
    <td>{{asset.name}}</td>
    <td>{{asset.obj_type.name}}</td>
    <td ng-init="loadLocation(asset)">
         {{asset.location.city}}
    </td>
    <td>{{asset.location.state}}</td>
</tr>

Code

$scope.loadLocation= function(asset) {
 Location.locationByIsprimary({assetId: asset.id}, function(result) {
            asset.location=result;
        });
    };

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

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