AngularJS- ng-repeat中的项目不会更新变量更新

时间:2017-06-12 12:35:35

标签: javascript angularjs arrays angularjs-scope

我有一个名为altSegments的数组,基于$ scope.firstSeg或$ scope.lastSeg,我想显示同一个数组的不同部分。在大多数情况下,我总是更改altSegments数组并且它更新正常,但是当我从相同的altSegments数组转到同一个数组但更改$ scope.firstSeg和$ scope.lastSeg时,它没有正确更新。

我怀疑它与altSegments没有改变有关,因此AngularJS决定再次检查代码并重新显示它是不值得的。我该如何解决这个问题?



<li ng-repeat="altseg in altSegments">
  <!-- For multiflight home to first -->
  <div ng-show="{{firstSeg}}" ng-repeat="flights in altseg.segment_details_1.leg_details">
    <p class="small dark">
      <strong>Flight:</strong> {{flights.Carrier}} {{ flights.FlightNumber}}
    </p>
    <p class="small dark">
      <strong>Departure:</strong> {{flights.OriginName}} | {{flights.DepartureTime | splitDT }}
    </p>
    <p class="small dark">
      <strong>Arrival:</strong> {{flights.DestinationName}} | {{flights.ArrivalTime | splitDT }}
    </p>
  </div>
  <!-- For multiflight last to home -->
  <div ng-show="{{lastSeg}}" ng-repeat="flights in altseg.segment_details_2.leg_details">
    <p class="small dark">
      <strong>Flight:</strong> {{flights.Carrier}} {{ flights.FlightNumber}}
    </p>
    <p class="small dark">
      <strong>Departure:</strong> {{flights.OriginName}} | {{flights.DepartureTime | splitDT }}
    </p>
    <p class="small dark">
      <strong>Arrival:</strong> {{flights.DestinationName}} | {{flights.ArrivalTime | splitDT }}
    </p>
  </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

ng-show是一个角度指令,用于评估角度代码;

因此;你不需要:ng-show="{{firstSeg}}"

重新加注:ng-show="firstSeg"

请在此处查看ng-show的完整文档:https://docs.angularjs.org/api/ng/directive/ngShow

答案 1 :(得分:0)

看起来您使用ng-show="{{firstSeg}}"这应该是ng-show="firstSeg" ..

如果仍然不起作用, 尝试在$ scope.apply()...

中更新控制器端的数据

例如: -

$scope.apply(function(){
  list = updated_list;    // put your updation of list here 
});