3

i have a collection of values whose structure lets assume to be

var a = [{id:1, value:12, name="one"}, {id:2, value:34, name="two"},...]

i wanted to display this in a series of controls so that user can change the values. but with that i also wanted to display original values which obviously shoudn't change.
i found out a way that is working and my code is something like this using ng-init

 <div ng-repeat="p in a">
  <div class="control-group" ng-if="p.value>0">
    <label class="control-label" ng-bind="p.name"></label>
    <div class="controls controls-row" ng-init="v=p.value">
      <input class="span1" value="{{v}}"/>
      <input type="number" ng-model="p.value" class="span2" />
    </div>
  </div>
</div>

being a complete newbie in angularjs i dont know what implications this might have as i have very little experience in thinking about $watch and performance.

Is it ok to do so?

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • 1
    would suggest just copying the whole array if you want to keep it...`var b= angular.copy(a)`. Can then match indexes for markup – charlietfl Nov 29 '13 at 06:22
  • @charlietfl - yes my first attempt was that. but still angular keeps watching the values when i know they wont be changing. – Parv Sharma Nov 29 '13 at 06:24
  • don't understand last comment – charlietfl Nov 29 '13 at 06:34
  • at first i did that.. to copy the array and then using it to bind values. But still if i cope an array using angular.copy() it creates a deep copy and hence angular js will continue to look for changes – Parv Sharma Nov 29 '13 at 09:47
  • you keep saying the same thing....and it's not making sense. A demo would help that presents your issue – charlietfl Nov 29 '13 at 14:18

1 Answers1

0

but with that i also wanted to display original values which obviously shoudn't change.

Use angular.copy(/* array */). It will create new copy (instance) of old array.

BTW a collection must be defined as $scope.a

Demo Fiddle

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225