Question: ng-repeat display key value in view

Question

ng-repeat display key value in view

Answers 3
Added at 2017-01-01 06:01
Tags
Question

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?

Answers to

ng-repeat display key value in view

nr: #1 dodano: 2017-01-01 06:01

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/

nr: #2 dodano: 2017-01-01 07:01

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/

nr: #3 dodano: 2017-01-01 07:01

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>
Source Show
◀ Wstecz