简体   繁体   English

ng根据网址重复不同的值

[英]ng-repeat different values based on url

I am trying to repeat values from array of objects based on current url. 我试图基于当前url从对象数组中重复值。 Let's say that when my url is this "localhost/#/scores/javelin" i want to repeat only javelin scores from my array. 假设当我的网址是“ localhost /#/ scores / javelin”时,我只想重复数组中的标枪得分。

My array looks like this: 我的数组如下所示:

$scope.ppl = 
            [
                {
                    firstname: 'Donald',
                    lastname: 'Trump',
                    from: 'USA',
                    run100m: ['1s','2s','3s'],
                    javelin: ['50m','20m','44m']
                },
                {
                    firstname: 'Foo',
                    lastname: 'Bar',
                    from: 'SWE',
                    run100m: ['1s','2s','3s'],
                    javelin: ['80m','10m','54m']
                },
            ];

There will be over 15 type of sports and unknown amount of people. 将有15种以上的运动类型和人数未知的人群。 I have no idea how i can do this without making huge amounts of arrays. 我不知道如何在不制作大量数组的情况下做到这一点。 I've been trying all kinds of things to do this but nothing have not worked. 我一直在尝试各种各样的方法来做到这一点,但是没有任何事情没有奏效。

This is what my repeat looks like: 这是我的重复内容:

<tr ng-repeat="x in ppl">
        <td>{{x.firstname}}</td>
        <td>{{x.lastname}}</td>
        <td>{{x.from}}</td>
        <td><span ng-repeat="y in x.run100m track by $index">{{ y }}</span>
        </td>
        <td><button>edit</button></td>
    </tr>

This code is working like this but only with run100m. 这段代码像这样工作,但仅适用于run100m。 If i change the url to /javelin, i want javelin scores inside of span. 如果我将网址更改为/ javelin,则希望标枪得分在span范围内。

Only way i can make this work is that i create 我可以做这项工作的唯一方法是我创造

<span ng-show=" URL == javelin"></span>

etc. but i am not sure if that is so smart way. 等等。但是我不确定这是否很聪明。 So my question is that how i can make this work smart way? 所以我的问题是,我怎样才能使这项工作变得聪明呢? the ppl structure doesn't have to be like that but it would be great if i can store all the information in 1 variable. ppl结构不必一定是这样,但是如果我可以将所有信息都存储在1个变量中,那就太好了。

Oh and i forgot to say that i know how i can get url inside of controller. 哦,我忘了说我知道如何在控制器内部获取url。 That is not the problem here. 这不是这里的问题。

So i want repeat based on this variable: 所以我想基于此变量重复:

var path = $routeParams.sport;

Use a property accessor in the expression: 在表达式中使用属性访问器:

<tr ng-repeat="x in ppl">
    <td>{{x.firstname}}</td>
    <td>{{x.lastname}}</td>
    <td>{{x.from}}</td>
    <td><span ng-repeat="y in x[eventType] track by $index">{{ y }}</span>
    </td>
    <td><button>edit</button></td>
</tr>

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

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