从ng-repeat中的子元素获取父属性

时间:2016-12-30 16:12:18

标签: javascript angularjs

$scope persons = [{"id":123, "name": "james",
    "score": [{
      "mark": 60,
      "grade": D
    }]
}]

我有2 ng-repeat。

<div ng-repeat="person in persons">
{{person.name}}
<span ng-repeat="scoreObj in person.score">
<p ng-click="something(person.id)"></p> <!-- won't work -->
{{person.id}} // work
</span>
</div>

我试图在person元素中获取person.id,它没有工作,任何想法?

1 个答案:

答案 0 :(得分:0)

它有效,你的json应该是,

<div id="div1">
    <button type="button">Button 1</button>
    <button type="button">Button 2</button>
    <button type="button">Button 3</button>
    <button type="button">Button 4</button>
</div>

<强>样本

 $scope.persons = [{
    "id": 123,
    "name": "james",
    "score": [{
      "mark": 60,
      "grade": 'D'
    }]
  }]
var testApp= angular.module('test',[])
angular.module('test').controller('testCtrl', function($scope) {
  $scope.persons = [{
    "id": 123,
    "name": "james",
    "score": [{
      "mark": 60,
      "grade": 'D'
    }]
  }]



})