0

To view my code follow this link

I've created a directive which handles an array of items (it will always be two because it's a "from" and "to" date pair).

I want to make the array items accessible as separate values for later use so I refer to the array items like this

  vm.data = ['data a', 'data b'];
  vm.separateData = vm.data[0];
  vm.otherData = vm.data[1];

When I implement a two way bind into the directive, the vm.data[0] and vm.data[1] references are updated but the vm.separateData and vm.otherData aren't.

Is there a way of making this work or should I just restructure the rest of my app (where needed) to accomodate for array items?

In my fiddle link (same as above) try changing the text input values and you'll see what I mean.

Snsa90
  • 151
  • 9

1 Answers1

1

vm.data[0] is a string and thus it is a primitive datatype in javascript which is immutable. So you bind the immutable String 'Data a' to vm.separateData, which is not a reference to data[0].

If you want to copy the reference to the array into vm.separateData try to wrap your strings in other javascript objects, e.g.

vm.data = [{"value":"Data a"}, {"value":"Data b"}]

and then you can reference

vm.separateData = vm.data[0];

and access the value via

vm.separateData.value
Thorsten
  • 91
  • 1
  • 3
  • valid point thanks! but it seems it's not quite working - have a look https://jsfiddle.net/Snsa90/gyf3dexa/1/ ... have I misinterpreted you ? – Snsa90 Jun 29 '16 at 09:12
  • maybe i should just restructure the queries i have in the rest of the app to take data[0] instead of variable values, that way it would work - i was just wondering is there a less risky workaround... – Snsa90 Jun 29 '16 at 09:13
  • You now still copy the immutable string, as you use data[0].value. Look here :https://jsfiddle.net/hn08wuhL/ – Thorsten Jun 29 '16 at 09:14