Javascript 使用对象键的ng开关
我想对一组对象进行分组Javascript 使用对象键的ng开关,javascript,angularjs,Javascript,Angularjs,我想对一组对象进行分组 var data = [ {term: 'baz', description: 'Patient'}, {term: 'bar', description: 'Patient'}, {term: 'foo', description: 'Doctor'} ] 现在,我想通过下拉列表按描述对这些数据进行分组。使用下划线。.groupBy我以 $scope.grouped = _.groupBy(data, 'description') {Patien
var data = [
{term: 'baz', description: 'Patient'},
{term: 'bar', description: 'Patient'},
{term: 'foo', description: 'Doctor'}
]
现在,我想通过下拉列表按描述对这些数据进行分组。使用下划线。.groupBy
我以
$scope.grouped = _.groupBy(data, 'description')
{Patient: [
{term: 'baz', description: 'Patient'},
{term: 'bar', description: 'Patient'}
]
},
{Doctor: [
{term: 'foo', description: 'Doctor'}
]
}
您可以将ng开关与对象键一起使用吗
<div ng-switch="grouped">
<span ng-switch-when="Patient">
<p>Patients</p> // only shown once to categorize the data
// ng-repeat in here
</span>
<span ng-switch-when="Doctor">
<p>Doctors</p> // only shown once to categorize the data
// ng-repeat in here
</span>
</div>
似乎您希望在
ng repeat
中使用此选项,在这种情况下,您可以使用对象键
<div ng-repeat="(group, data) in grouped">
<div ng-switch="group">
<div ng-switch-when="Patient">
<h1>Patients</h1>
<p ng-repeat="patient in data">{{patient.term}}</p>
</div>
<!-- same for doctor -->
</div>
</div>
患者
{{patient.term}
您似乎希望在ng repeat
中使用此选项,在这种情况下,您可以使用对象键
<div ng-repeat="(group, data) in grouped">
<div ng-switch="group">
<div ng-switch-when="Patient">
<h1>Patients</h1>
<p ng-repeat="patient in data">{{patient.term}}</p>
</div>
<!-- same for doctor -->
</div>
</div>
患者
{{patient.term}