Note that there are some explanatory texts on larger screens.

plurals
  1. POAngular: How to bind to an entire object using ng-repeat
    primarykey
    data
    text
    <p>I'm just beginning to experiment in Angular, and confused about how best to approach binding using ng-repeat. I basically get the point about ng-repeat creating a child scope. My problem is much more basic :) For html like this:</p> <pre><code>&lt;div ng-controller="swatchCtrl" class="swatch-panel"&gt; Swatches &lt;ul&gt; &lt;li ng-repeat="swatch in swatchArray" class="swatch"&gt; &lt;input type="radio" name="swatches" ng-model="$parent.currentSwatch" value="{{swatch}}" &gt; &lt;label class="swatch-label"&gt; &lt;div class="swatch-color" style="background-color: #{{swatch.hexvalue}};"&gt;&lt;/div &gt;&lt;span class="swatch-name"&gt;{{swatch.colorName}}&lt;/span&gt; &lt;/label&gt; &lt;/li&gt; &lt;/ul&gt; currentSwatch is: &lt;pre&gt;{{currentSwatch | json}}&lt;/pre&gt; currentSwatchObj is: &lt;pre&gt;{{currentSwatchObj | json}}&lt;/pre&gt; how do I tell this to fire?? swatchArray is: &lt;pre&gt;{{swatchArray | json}}&lt;/pre&gt; &lt;/div&gt; </code></pre> <p>and javascript like this:</p> <pre><code>function swatchCtrl($scope) { $scope.swatchArray = [ {colorName:'Red', hexvalue: 'ff0000', selected: 'false'}, {colorName:'Green', hexvalue: '00ff00', selected: 'false'}, {colorName:'Blue', hexvalue: '0000ff', selected: 'false'} ]; $scope.currentSwatch = {}; } </code></pre> <p><a href="http://jsfiddle.net/8VWnm/" rel="nofollow">http://jsfiddle.net/8VWnm/</a></p> <p>I want to:</p> <p>a) When the user clicks on a radio button, I want it to set both the colorName and the hexvalue properties of the currentSwatch object. Right now the binding seems to be giving me a stringified object from the array. How do watch the return of currentSwatch so I can parse it back to an available object? Simple, I know, but what am I missing?</p> <p>b) When the user clicks on a radio button, I think I want that to set the value of the corresponding "selected" key in the original array to "true". Vice versa for unchecking. Let's say that only one swatch can ever be selected at a time in the palette. (I would like in theory to be able to iterate through the array later on, on the supposition that the different keys and values are likely to sometimes not be unique.)</p> <p>This kinda stuff is super easy with jquery methods, but I'd like to learn the idiomatic angular way. Thanks in advance for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload