【问题标题】:AngularJS ngOptions track by does not set entire object to ngModelAngularJS ngOptions track by 不会将整个对象设置为 ngModel
【发布时间】:2016-06-17 07:09:10
【问题描述】:

这是我的 json 对象:

 $scope.todos = [{
    name: 'angular',
    field: ['a', 'b', 'c', 'd'],
    id: 1
  }, {
    name: 'asd',
    field: ['a', 'b', 'c', 'd', 'e'],
    id: 2
  }];

我给选择为:

<select ng-model="dashboard.type" ng-options="item as item.name for item in todos track by item.name"></select>

Now when an option is chosen i want another select which iterates upon ngModel of previous select.下一个看起来像:

<select ng-model="dashboard.label" ng-options="item as item for item in dashboard.type.field"></select>

当我手动选择选项时它工作正常但是当它们是我脚本中dashboard.type中的值(例如:$scope.dashboard.type.name = 'qwe')时,它使用track by选择选项并将其保存在dashboard.type中,而不是保存整个对象,它只是保存track by 选项中使用的值。

dashboard.type 的值:

  1. 当我手动选择选项时: {"name":"qwe","field":["a","b","c","d","f"],"id":3}
  2. 使用跟踪方式时: {"name":"qwe"}

注意:我不能使用 track by item 。它必须是对象item 的属性。可以是nameid

这是plnkr

编辑:

正如许多人所指出的,我想澄清一下,我不能将对象初始化为todos 列表的任何值。这是因为dashboard.type.name 的值每次都会不同,而todos 列表的对象数量从 10 到 100 不等。现在我必须迭代列表todos 检查todos[index].name==someName 是否并分配相关对象。我正在积极尝试避免这种情况(因为必须有更好的方法)。

注意:我也尝试使用ng-change 并将实际对象分配给dashboard.type.name,但这仅在手动选择选项时有效,这似乎已经正常工作。使用track by时不分配对象。

重要提示: $scope.dashboard.type.name = 'qwe' 只是一个示例。每次加载页面时,$scope.dashboard.type.name 的值可能会发生变化。

PS:我也应该早点提到这一点。对不起,我的错!

【问题讨论】:

  • 只需像这样更改您的初始化 $scope.dashboard={}; $scope.dashboard.type ={} $scope.dashboard.type ={ name: 'qwe', field: ['a', 'b', 'c', 'd', 'f'], id: 3 } ;
  • 如果我可以在不遍历 todos 列表的情况下完成最后一部分,那么它会起作用。 PS:我已经更新了问题,请再次检查。
  • 在这种情况下调用 ng-init 上的函数并根据您想要的值在那里初始化
  • 试过了,不行,可以给我看一下吗??

标签: angularjs angular-ngmodel ng-options angularjs-track-by


【解决方案1】:

这个问题不是因为track by。通过执行$scope.dashboard.type.name = 'qwe' 设置默认值,会使ng-model 看起来像$scope.dashboard.type = {name : 'qwe'}。不要以这种方式选择默认值,而应使用$scope.dashboard.type = $scope.todos[2];。这将确保整个对象设置为$scope.dashboard.type

Edited plunker

【讨论】:

    【解决方案2】:

    要使用默认值初始化模式,您可以使用 ng-init。例如

     <select ng-init="dashboard.type = todos[2]" ng-model="dashboard.type" ng-options="item as item.name for item in todos track by item.name">
      </select>
    

    【讨论】:

      【解决方案3】:

      在 ng-init 上调用这个函数 在html中

      ng-init ="initialize();"
      

      在js中

      $scope.dashboard={};//initialize object
      $scope.dashboard.type ={}//initialize object
      
      $scope.initialize =function(){
      
      
      //no need to iterate if it is certain that your Id and index is always same suppose your initial id is 3
      
      //then
      var currId =3;//suppose initial id is there
      $scope.dashboard.type =$scope.todos[currId]
      
      }
      

      这里是它的代码笔 http://codepen.io/vkvicky-vasudev/pen/MeexmK

      【讨论】:

      • 我刚刚给出了一个随机条件,如果你可以使用你自己的条件
      • 谢谢,但在这里您使用.forEach() 循环遍历列表并将有效对象设置为$scope.dashboard.type,因为我提到此解决方案有效,但我的问题是:有没有更好的方法它不是循环遍历列表吗?
      • 如果您知道最初要选择的 id 并且确定您的 id 始终与数组索引匹配,则可以这样做,请找到更改后的代码笔
      猜你喜欢
      • 2014-06-17
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多