Jay
Jay

Reputation: 165

ng-repeat display key value in view

http://jsfiddle.net/qq2L34eb/1/

I want to display Monday but the value attr is equal to one using ng-repeat.

$scope.days = [{
    "Mondays": 1
}, {
    "Tuesdays": 2
}];

But I got the entire object instead.

http://jsfiddle.net/qq2L34eb/1/

Any clue?

Upvotes: 3

Views: 1157

Answers (3)

Mohammad Sadiqur Rahman
Mohammad Sadiqur Rahman

Reputation: 5523

According to your js fiddle in html write---

<span ng-repeat="pp in days">
    <input type='checkbox' value="{{pp.key}}" check-list='checked_days'> {{pp.value}}
</span>

In Controller---

 $scope.days = [{
    "key": "1","value":"Monday"
  }, {
    "key": "2","value":"Tuesday"
  },  ]

you can find result like this--

enter image description here

js fiddle http://jsfiddle.net/qq2L34eb/2/

Upvotes: -1

Rehban Khatri
Rehban Khatri

Reputation: 954

2 things -

1) Your object array can be changed.

Your $scope.days is an array, it should be object instead. Like this

  $scope.days = {
    "Mondays": 1
  , 
    "Tuesdays": 2
  }

See fiddle

2) If you don't wish to change object array then add another ng-repeat within that like this. Like @Sajeetharan mentioned -

<span ng-repeat="(key, value) in days">
<span ng-repeat="(key, value) in value">
               <input type='checkbox' value="{{value}}" check-list='checked_days'> {{key}}
</span>
</span>

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222522

You need to use another ng-repeat inside,

<div ng-controller="MyCtrl">
  <span ng-repeat="(key, value) in days">
    <span ng-repeat="(key, value) in value">
                   <input type='checkbox' value="{{value}}" check-list='checked_days'> {{key}}
    </span>
   </span>
</div>

DEMO

http://jsfiddle.net/sajeetharan/2obrb921/

Upvotes: 3

Related Questions