当ng模型更改时,形成引用旧内存的单选按钮-中断ng检查

Form radio buttons referencing old memory when ng-model is changed - breaks ng-check

本文关键字:ng 内存 单选按钮 检查 中断 引用 模型      更新时间:2023-09-26

角度1.*

我有:

  <div class="filter-column">
    <div class="filter-title">Market</div>
    <div class="bottom-line"></div>
    <div class="overflow-container">
        <input type="radio" value="all" name="marketRadio" 
          ng-checked="true" class="resetAll" ng-model="filter.markets" 
          ng-change="radioMap('market')" checked>All
      <div ng-repeat="choice in markets| orderBy: 'name'">
        <input type="radio" value="{{choice.name}}" name="marketRadio"
          ng-change="radioMap('market')" ng-model="filter.markets" >
        {{choice.description}}
      </div>
    </div>

在控制器中我有:

  var ppvFilter = {
    regions: [],
    markets: [],
    dealers: []
  };

  $scope.$watchCollection(function() {
    return ppvFilter;
  },
    function(newValue) {
      $scope.regions = newValue.regions;
      $scope.markets = newValue.markets;
      $scope.dealers = newValue.dealers;
  });

当我用ppvFilter.markets.length = 0; ppvFilter.markets = ppvFilter.markets.concat(['a', 'b', 'c'])以程序方式刷新单选按钮列表(而不是页面刷新(时,单选按钮选项的列表会在gui中更新。但是,ng-checked="true"不再适用于all,并且在列表更新后将取消选中。

我怀疑这是因为有角度的形式引用了旧的记忆,尽管它显示了新的单选按钮列表。

从ngChecked:的角度文档中注意,此指令不应与ngModel一起使用,因为这可能会导致意外行为

我想明白了。。。。默认项目必须在ng重复中。ng-check在现实生活中用处不大。

参考:

使用ng repeat 时默认选中单选按钮