【问题标题】:How to pass dynamic variable inside ng-repeat如何在 ng-repeat 中传递动态变量
【发布时间】:2019-03-28 11:08:01
【问题描述】:

我的函数中有一个名称为当前 id 的数组:

$scope.expand = function(hodid, supid, leader_id, staff_id, importance){
  var tr_id = 'expand_tree_'+hodid;

在控制台输出中它看起来像这样:expand_tree_1000321 这就是我的数组最终的样子:

  $scope[tr_id] = {firstLevel:[$scope.firstLevelLst], secondLevel:[$scope.secondLevelLst], thirdLevel:[data_third]};
};  

现在我想在ng-repeat 中打印这个$scope[tr_id],其中tAttrs.treeid 是所选用户的ID:

<div  ng-repeat="first in ['expand_tree_'+tAttrs.treeid].firstLevel['0']">
  {{first.username}}
</div>

但它不打印任何东西。如何使用动态变量在ng-repeat 内打印范围数组?

【问题讨论】:

  • 我编辑了我的答案

标签: javascript html angularjs angularjs-ng-repeat


【解决方案1】:

首先,您似乎正在使用$scope 作为您的数据对象。由于几个原因,这并不好。在作用域上使用对象,例如:

$scope.data = {};

并像这样存储数据:

$scope.data[tr_id] = {
    firstLevel: [$scope.firstLevelLst],
    secondLevel: [$scope.secondLevelLst],
    thirdLevel: [data_third]
};

但不要将属性包装在数组中,因为它们已经是数组:

$scope.data[tr_id] = {
    firstLevel: $scope.firstLevelLst,
    secondLevel: $scope.secondLevelLst,
    thirdLevel: data_third
};

然后就可以循环遍历firstlike:

<div ng-repeat="first in data['expand_tree_' + tAttrs.treeid].firstLevel">
    {{first.username}}<br />
</div>

这是Working Fiddle

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多