简体   繁体   English

从ng-repeat中选择时,仅显示json中的一项

[英]Showing only one item from json when selected from ng-repeat

I have a list of questions being pulled in and when they are clicked I want it to show just that question along with the possible answers but I cant work out how to do that. 我列出了要提出的问题,当单击它们时,我希望它仅显示该问题以及可能的答案,但我无法解决该怎么做。

I have made an example plunker here: 我在这里做了一个例子:

http://plnkr.co/edit/iFOLsZawzJURegD2YZ1x?p=preview http://plnkr.co/edit/iFOLsZawzJURegD2YZ1x?p=preview

To keep SO happy, here is my app.js 为了让大家开心,这是我的app.js

angular.module('foo', ['ui.router'])

.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/q1');

    $stateProvider

    .state('results', {
        url: '/results',
        templateUrl: 'results.html'
    })

    .state('list', {
        url: '/questions',
        templateUrl: 'list.html',
        controller: 'questionsCtrl'
    })

    .state('question', {
        url: '/questions/:questionID',
        templateUrl: 'question.html',
        controller: 'questionsCtrl'
    })

})

.controller('questionsCtrl', function($scope, $stateParams) {

    $scope.questionID = $stateParams.questionID;
    $scope.list = [
        {
            "award":"Best in the world",
            "nominees": [
                {
                    "name":"Bob",
                    "age":"53"
                },
                {
                    "name":"Hilary",
                    "age":"44"
                }
            ]
        },
        {
            "award":"Best in the land",
            "nominees": [
                {
                    "name":"Sally",
                    "age":"22"
                },
                {
                    "name":"John",
                    "age":"66"
                }
            ]
        }
    ];

});

Does this work for you? 这对您有用吗?

list.html list.html

<ul>
    <li ng-repeat="qwerty in list">
        <a ui-sref="question({questionID: {{ $index }} })">Question {{$index + 1}}. {{ qwerty.award }}</a>
    </li>
</ul>

question.html Question.html

<div>
    {{ questionID }} <br>
    <li ng-repeat="qwerty in list" ng-show="$index == questionID">
      {{ qwerty.award }}
      <div ng-repeat="nom in qwerty.nominees" style="margin-left:8px">
        {{ nom.name }}
      </div>
    </li>
</div>

I have updated your plunker, please check it here http://plnkr.co/edit/mubfDn3zRa0g9nkCK0ya?p=preview 我已经更新了您的监听器,请在这里检查它http://plnkr.co/edit/mubfDn3zRa0g9nkCK0ya?p=preview

I have changed your controller to assign the question to the scope using state param id 我已更改您的控制器以使用状态参数ID将问题分配给范围

.controller('questionsCtrl', function($scope, $stateParams) {

$scope.list = [
    {
        "award":"Best in the world",
        "nominees": [
            {
                "name":"Bob",
                "age":"53"
            },
            {
                "name":"Hilary",
                "age":"44"
            }
        ]
    },
    {
        "award":"Best in the land",
        "nominees": [
            {
                "name":"Sally",
                "age":"22"
            },
            {
                "name":"John",
                "age":"66"
            }
        ]
    }
];
//alert($stateParams.questionID);
$scope.question = $scope.list[$stateParams.questionID];

}); });

In question .html use this 有问题的.html使用此

<br>
<div>
    Q{{ questionID+1 }}.{{list[questionID].award}} ?
</div>
<br>
<div ng-repeat="nominee in list[questionID].nominees">
  {{$index+1}}.{{nominee.name}},{{nominee.age}}
</div>

I have changed your questionID to represent list index instead of using q prefix 我已更改您的questionID以表示列表索引,而不是使用q前缀

Also make questionId int 还要使questionId为int

 $scope.questionID = parseInt($stateParams.questionID);

Working Demo: http://plnkr.co/edit/qIb07wvqciCfc1VarVnO?p=preview 工作演示: http : //plnkr.co/edit/qIb07wvqciCfc1VarVnO?p=preview

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

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