2014-08-29 64 views
0

選擇當我新的角度和剛開始我的第一個應用程序,試圖與它自己熟悉,但我都碰了壁,我希望有人能輕易回答JSON只有一個項目。角 - 顯示來自NG-重複

我拉進了一個問題列表,並單擊時,我希望它有可能的答案一同顯示只是這個問題,但我不能工作,如何做到這一點。

我在這裏做出了表率plunker:

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

要保持這麼開心,這是我的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" 
       } 
      ] 
     } 
    ]; 

}); 

不勝感激任何指針。

A.

+0

您plnkr鏈接斷開,僅供參考。 – 2014-08-29 13:24:43

+0

真的嗎?爲我工作 – Adi 2014-08-29 13:29:01

回答

0

我已經更新您的plunker,請點擊此處查看http://plnkr.co/edit/mubfDn3zRa0g9nkCK0ya?p=preview

我已經改變了你的控制器分配問題向範圍使用狀態PARAM 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]; 

});

0

這是否適合您?

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

<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> 
0

問題.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> 

我已經改變了你的questionID來表示,而不是用品質前綴

也使questionId詮釋

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

工作演示列表索引: http://plnkr.co/edit/qIb07wvqciCfc1VarVnO?p=preview