buberdds / angular-bootstrap-colorpicker

Native AngularJS colorpicker directive. No dependency on jQuery or jQuery plugin is required.
MIT License
419 stars 226 forks source link

Model does not update in ng-repeat #53

Closed chiragchamoli closed 10 years ago

chiragchamoli commented 10 years ago

I have a array of hex colors ["#a17878","#222222"] when you loop through these with a ng-repeat.

                   <li  ng-repeat="color in  colors track by $index">
                       <div style="width:60px;border:3px solid {{color}};">
                            <input colorpicker="hex" type="text" ng-model="color" />
                      </div>
                  </li>

In this case the color picker won't update the model.

boxxxie commented 10 years ago

don't use primitives as models.

chiragchamoli commented 10 years ago

@boxxxie example above is simplified. But it won't still work in ng-repeat.

boxxxie commented 10 years ago

can you make a plnkr?

chiragchamoli commented 10 years ago

Please see http://plnkr.co/edit/xtk62ub2d71BX5uEzPSZ?p=preview.

If you change the color, it won't update the model.

boxxxie commented 10 years ago

sorry, having trouble loading plnkr ATM (in china). try ng-model="$parent.color"

ng-repeat creates a new scope.

buberdds commented 10 years ago

@chiragchamoli just like @boxxxie said ng-repeat creates a new scope for each element in a loop.

update your html markup to something like this

    <li  ng-repeat="c in  colors track by $index">
            <input style="width:60px;" colorpicker="hex" type="text" ng-model="$parent.colors[$index]" /> 
    </li>
chiragchamoli commented 10 years ago

@buberdds thanks this method works.