简体   繁体   English

AngularJs:奇怪的结果是迭代键值对

[英]AngularJs:Strange result in iterating key value pair

I have an array in angular as follows 我有一个角度数组如下

$scope.arr={
    "abc/xyz/../": [
        "a1.txt",
        "a2.txt",
        "a3.txt"
    ],
    "": [
        "no-folder.txt"
    ],
    "abc1/xyz/../": [
        "a4.txt",
        "a5.txt",
        "a6.txt"
    ]
}

i want to iterate through this array and need to get the results as follows 我想遍历此数组,需要获得如下结果

    "a1.txt",
    "a2.txt",
    "a3.txt" 

and

"no-folder.txt"

and

            "a4.txt",
            "a5.txt",
            "a6.txt"

i have tried as follows 我已经尝试过如下

<div ng-repeat="test in arr track by $index">
    <h3 ng-repeat="(key, value) in test">
        {{value}}
    </h3>
</div>

but i am getting strange results as each character is coming as result . 但是我得到了奇怪的结果,因为每个角色都来了。 can u pleas help. 您可以帮忙吗?

update please look to this fiddle https://jsfiddle.net/771voyj6/8/ 更新请查看此小提琴https://jsfiddle.net/771voyj6/8/

I suspect that the JSON you are getting is not valid JSON. 我怀疑您获取的JSON无效。 In second second property of your object is "" (blank) which would be invalid JSON. 对象的第二个第二个属性是"" (空白),它将是无效的JSON。

JSON JSON格式

{
    "abc/xyz/../": [
        "a1.txt",
        "a2.txt",
        "a3.txt"
    ],
    //in below line json has no key
    "": [
        "no-folder.txt"
    ],
    "abc1/xyz/../": [
        "a4.txt",
        "a5.txt",
        "a6.txt"
    ]
}

Update 更新资料

Apology for having wrong conceptualization regarding JSON, above JSON is valid JSON (but technically JSON shouldn't have blank key). 抱歉,关于JSON的概念错误,在JSON之上是有效的JSON(但从技术上讲,JSON不应包含空白键)。 I tried the above code in Fiddle which is working fine. 我在Fiddle中尝试了上面的代码,效果很好。

Working Fiddle(Angular 1.4.8) 工作小提琴(角度1.4.8)

I don't know if Pankaj is right regarding the empty key value. 我不知道Pankaj关于空键值是否正确。 But certainly your $scope.arr is not an array but an object. 但是可以肯定的是,您的$scope.arr不是数组而是对象。 As such your code should be: 因此,您的代码应为:

<div ng-repeat="(key, value)in arr track by $index">
    <h3 ng-repeat=" item in value">
        {{item}}
    </h3>
</div>

I don't know what you want to do but you can create your logic something like this it might work for you ,its some hardcoded work but change it accordingly 我不知道您想做什么,但是您可以创建类似这样的逻辑,它可能对您有用,它的一些硬编码的工作,但是要相应地进行更改

       $scope.test = []

       $scope.test.push(arr["abc1/xyz/../"]+arr[""]+arr["abc1/xyz/../"]);

Use test in ng-repeat .. :) 在ng-repeat中使用test .. :)

or you can do what i know with your code use ng-repeat like this for your code: 或者您可以使用代码执行ng-repeat来执行我所知道的代码:

<div ng-repeat="test in arr">
            <div ng-repeat="test2 in test">
              {{test2}}
              </div>

You need to replace your HTML code with the following: 您需要使用以下代码替换HTML代码:

<div ng-repeat="test in arr track by $index"> <div ng-repeat="item in test"> {{item}} </div> </div>

I am getting the result. 我得到结果。 Please check the following JSFiddle: 请检查以下JSFiddle:

http://jsfiddle.net/akonchady/a25r1mm4/3/ http://jsfiddle.net/akonchady/a25r1mm4/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM