2014-12-03 14 views
0

使用以下JSON作为例子并假设我已经在这个数据等于设置为$scope.people如何创建从上按钮嵌套JSON阵列数据嵌套纳克重复点击

[ 
    { 
     "personId": 1, 
     "name": "Thomas", 
     "age": 39 
     "friends": [ 
      { 
       "friendId": 1, 
       "nickName": "Lefty" 
      }, 
      { 
       "friendId": 2, 
       "nickName": "Morty" 
      }, 
      { 
       "friendId": 3, 
       "nickName": "Gomer" 
      } 
     ] 
    }, 
    { 
     "personId": 2, 
     "name": "George", 
     "age": 27, 
     "friends": [ 
      { 
       "friendId": 1, 
       "nickName": "Tommy" 
      }, 
      { 
       "friendId": 2, 
       "nickName": "Bobby" 
      }, 
      { 
       "friendId": 3, 
       "nickName": "Joe" 
      } 
     ] 
    } 
] 

我动态创建按钮为每个人。

<div class="form-group" data-ng-repeat="person in people"> 
    <button type="button" class="btn btn-default form-control" 
     data-ng-click="friends(person.personId)"> 
      {{person.name}} 
    </button> 
</div> 

我试图找出是如何根据按钮的对象数据加载到引导面板(见下HTML)点击:

<div class="panel panel-primary" data-ng-hide="!friends"> 
    <div class="panel-heading"> 
     <h4 class="panel-title">{{person.name}}<span class="pull-right">{{person.age}}</span></h4> 
    </div> 
    <div class="panel-body"> 
     <table class="table table-striped"> 
      <tr data-ng-repeat="friend in person.friends"> 
       <td>{{$index + 1}}</td> 
       <td>{{friend.nickName}}</td> 
      </tr> 
     </table> 
    </div> 
</div> 

我在想,我的控制器上,我设置了$scope变量=朋友阵列($scope.friends),但我不确定如何根据personId做到这一点。

回答

1

您需要创建一个$ scope变量来保存selectedPerson并在clickin时将正确的人员分配给所选人员克按钮

$scope.SelectMe = function (p) { 
     $scope.selectedPerson = p; 
    } 

然后,您可以填充你的面板

时,我已经把一个快速捣鼓你引用selectedPerson。 http://jsfiddle.net/cseignc/wt8w01b4/

让我知道如果这是你所需要的。

+0

是真的,但他想用id做他不? – Florian 2014-12-03 20:10:38

0

你可以使用一本字典,而不是数组: 使用indexBy()函数underscore.js http://underscorejs.org/#indexBy

,那么你可以这样做

$scope.peopleById = _.indexBy(people, "personId"); 

,并在HTML文件中

<tr data-ng-repeat="(id, friend) in person.friends"> 
<!-- here you can do peopleById[id] -->