【问题标题】:angular dynamic property name in nested ng-repeat嵌套 ng-repeat 中的角度动态属性名称
【发布时间】:2017-02-14 14:43:54
【问题描述】:

我想知道有没有办法在嵌套的 ng-repeat 中添加动态生成的名称,例如:

    <div ng-repeat="x in JSONfile">
      <div ng-repeat="i in x.name">
         <span><a ng-href="{{i.link}}">{{i.name}}</a></span>
      </div>
    </div>

JSONfile:返回一些名称,

x.name 是从提到的 JSONfile 动态生成的,它应该用作像“NAME”这样的纯文本,如果我添加 NAME 而不是 i.name,我会加载 json 文件,但我希望它自动加载,因为我不知道哪个名字会先出现。

i.name 返回:

n

a

m

e

不是应该的“NAME”..

所以,问题是,有没有办法告诉 Angular 我希望这个动态生成的值在我输入时被查看?

PS。 x.name 加载一个 JSON 文件,其中包含一些关于某个人的信息。

如果我输入 ng-repeat="i in Tom" 它将返回 json,但使用 x.name 它不起作用。

谢谢!

编辑(添加 json):

var brojVijesti = [ ];
$scope.JSONfile = brojVijesti;

            // LNG Json
            $http.get("/LEADERBOARDv2/jsons/LNG.php").then(function(response) {
                var LNG = response.data.LNG;
                $scope.LNG = LNG;
                $scope.LNGbroj = LNG.length;
                brojVijesti.push({"name":"LNG", "number":LNG.length});
            });

            // DT JSON
            $http.get("/LEADERBOARDv2/jsons/DT.php").then(function (response) {
                var DT = response.data.DT;
                $scope.DT = DT;
                $scope.DTbroj = DT.length;
                brojVijesti.push({"name": "DT", "number": DT.length});
            });

【问题讨论】:

  • 什么是json样本格式?
  • 您介意分享 JSON 吗?
  • 你为什么不直接使用x.name你为什么要循环名字?
  • @manish :我需要从 $scope 获取 LNG 或 DT。 LNG 和 DT 以及许多其他的,都是动态生成的。

标签: javascript angularjs json ng-repeat


【解决方案1】:

我相信你想要i in x 而不是i in x.name

编辑:然后您可以使用 $parse 依赖项,该依赖项将从 $scope 中获取该特定名称的变量。

<div ng-repeat="x in JSONfile">
  <div ng-repeat="i in x">
    <span><a ng-href="{{i.link}}">{{getVariable(i.name)}}</a></span>
  </div>
</div>

JS:

$scope.getVariable = function(variableName) {
   //evaluate the variable name in scope's context.
   return $parse(variableName)($scope); 
}

【讨论】:

  • 我想要 x.name 中的 i,因为这会在循环的第一轮中给出 x 的名称列表。
  • @NicoLetterman 那么您是否尝试获取“DT”的名称,然后调用 $scope.DT 变量以获取在“DT”/“LNG”中找到的名称列表?
  • 是的,这正是我想要实现的目标。
  • 非常感谢!这就是我一直在寻找的。 :)
猜你喜欢
  • 2015-04-19
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多