【问题标题】:Form radio buttons referencing old memory when ng-model is changed - breaks ng-check更改 ng-model 时形成引用旧内存的单选按钮 - 中断 ng-check
【发布时间】:2017-03-16 06:54:58
【问题描述】:

角度 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,并且在列表更新后未选中。

我怀疑这是因为 Angular 表单正在引用旧内存,即使它正在显示新的单选按钮列表。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    我想通了....默认项目必须在 ng-repeat 中。 ng-check 在现实生活中用处不大。

    参考:

    Radio button checked by default when using ng-repeat

    【讨论】:

      【解决方案2】:

      来自 ngChecked 上的 documentation 角度:请注意,此指令不应与 ngModel 一起使用,因为这可能会导致意外行为。

      【讨论】:

      • 即使没有 ng-model 和 ng-change,行为仍然相同。
      • 有什么办法可以把它放在一个 plunker 或 fiddle 中?
      猜你喜欢
      • 2013-12-13
      • 2016-01-30
      • 2018-08-05
      • 2014-07-12
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多