【问题标题】:Variable being overriden in for loop在 for 循环中被覆盖的变量
【发布时间】:2015-06-29 15:00:58
【问题描述】:

对于每个事件,它们的位置都不同。但是,当它显示在 UI 上时,所有位置都已被最后一次 API 调用覆盖。我怎样才能阻止这种情况发生?

$scope.eventLoc = '';
    API.get({entity: 'organisation', entity_id: oid, property: 'campaign', property_id:cid, property2:'events'}, function(resp) {
        $scope.orgEvents = resp.data;
        for (i in resp.data) {
            ceid = resp.data[i].CampaignEventID;
            lid = resp.data[i].LocationID;
            API.get({entity: 'location', entity_id: lid}, function(respLocation){
                $scope.eventLoc = respLocation.data;
            })
        }
    });

<li ng-repeat="event in orgEvents track by $index">
    <h2>{{event.Name}}</h2>
    {{eventLoc.Address1}}
</li>

【问题讨论】:

    标签: javascript angularjs for-loop


    【解决方案1】:

    只需将您的代码更改为以下内容:

    //$scope.eventLoc = ''; //this can be removed
        API.get({entity: 'organisation', entity_id: oid, property: 'campaign', property_id:cid, property2:'events'}, function(resp) {
            $scope.orgEvents = resp.data;
            angular.forEach($scope.orgEvents, function(orgEvent) {
                //ceid = orgEvent.CampaignEventID; //this is not being used?
                lid = orgEvent.LocationID;
                API.get({entity: 'location', entity_id: lid}, function(respLocation){
                    orgEvent.eventLoc = respLocation.data;
                });
            });
        });
    
    <li ng-repeat="event in orgEvents track by $index">
        <h2>{{event.Name}}</h2>
        {{event.eventLoc.Address1}}
    </li>
    

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多