Ali-Alrabi
Ali-Alrabi

Reputation: 1698

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

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

Upvotes: 1

Views: 682

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

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.

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

Upvotes: 2

Related Questions