malik_curtin malik_curtin - 4 years ago 115
Javascript Question

Printing an array of objects using ng-repeat

I have a array of objects like below .

Signatures = [{
"Name": "Person 1",
"Status": "New"
}, {
"Name": "Person 2",
"Status": "New"
}, {
"Name": "Person 3",
"Status": "Completed"
}, {
"Name": "Person 4",
"Status": "Cancelled"
}];


I want to print the list by the "Name" of each of these objects in the following manner based on the Status.

New : Person 1 , Person 2
Completed : Person 3
Cancelled : Person 4


I have tried doing that with 3 ng-repeats . each containing a ng-if for comparing whether the Status is New , Completed or Cancelled . But i am looking for a better solution . Thanks in advance for any help

here is the JSfiddle http://jsfiddle.net/HB7LU/21355/

Answer Source

Modify you dom to use filter

<div ng-controller="MyCtrl">
<div>
New 
  <span ng-repeat="signer in Signatures | filter : {Status : 'New'}">
      <span>
                  <b>{{signer.Name}}</b>
      </span>
  </span>
</div>

<div>
Completed 
  <span ng-repeat="signer in Signatures | filter : {Status : 'Completed'}">
      <span>
                  <b>{{signer.Name}}</b>
      </span>
  </span>
</div>

<div>
Cancelled 
  <span ng-repeat="signer in Signatures | filter : {Status : 'Cancelled'}">
      <span>
                  <b>{{signer.Name}}</b>
      </span>
  </span>
</div>

</div>

This will work as expected.

See jsfiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download