如何在ng-repeat下调用一个函数?

how to call a function under ng-repeat?

我正在为我的项目使用 angularjs 1 和节点 js。

我在这里展示使用 ng-repeat.Each 的产品我可以选择在社交网络中分享。我需要获取每个共享计数,因此我需要调用一个函数来获取相同的共享计数。 我试图调用函数 ng-init 但对于所有产品它都采用最后一个产品计数

这是我的示例代码

     <div ng-repeat="(key,popc) in product" class="courses-outer-div">       
          <div class="courses-text-div">
          <div class="course-title-div">
          {{popc.Title}}
          </div><br />
          <span class="ash-txt-sml">{{popc.ListFee|currency:"₹":0}}</span><br/>

<div class="common-left pad-top-10"><span class="blue-txt">Share</span><br />
<a target="_blank"  href="http://facebook.com/sharer.php?u=url&i=imagurl" target="_blank">
<img src="images/share-fb.png" width="24" height="23" alt="" /></a>
<div ng-init="getfbshare(popc.id)">{{fbcount}}</div>
</div>
</div></div>

Controller.js

$scope.getfbshare = function(id)
    {       
        $http.get('/api/fbcount'+id).then(function(response)
        {
            alert("f"+response.data.share.share_count)
            $scope.fbcount = response.data.share.share_count;           
        });
    };

警报显示正确的值。

因为 ng-repeat $scope.fbcount 取最后一个值。

请帮我找出解决办法

您的 getfbshare 方法在范围 ($scope.fbcount) 上设置了一个固定值,该值将被对该函数的任何后续调用覆盖。

您可以传递整个 popc 对象,而不是将 popc.id 传递给您的函数,然后,您可以在响应中修改该对象,如下所示:

$scope.getfbshare = function(popc)
{       
    $http.get('/api/fbcount'+popc.id).then(function(response)
    {
        popc.fbcount = response.data.share.share_count;           
    });
};

那么,在您看来:

<div ng-init="getfbshare(popc)">{{popc.fbcount}}</div>

理想情况下,您会在应用程序生命周期的不同部分调用该方法,而不是 ng-init