0

in AngularJs I have two arrays: One with weeks and one with occupancy-numbers. Now at first I'm iterating over the week-array in a table:

<tr ng-repeat="week in weeks">
<!-- content for every week with occupancy-array -->
</tr>

In this table I have implemented a second ng-repeat for the occupancy-array, with the following structure per week:

Array[WeekId] = {early: null, late: null, night: null}

So for 3 weeks, the object exists 3 times.

The objects should be manipulatable for every week with an input-field. So my HTML is:

<tr ng-repeat="week in weeks">
    <td ng-repeat="(title, value) in occupancies[week.id]">
        <input class="form-control" ng-model="value"/>
    </td>
</tr>

But in this case, I have a problem by reaching the new values, because the array won't be changed in scope.

So then I decided to change my 2nd ng-repeat to this:

<td><input class="form-control" ng-model="occupancies[week.id].early"/></td>
<td><input class="form-control" ng-model="occupancies[week.id].late"/></td>
<td><input class="form-control" ng-model="occupancies[week.id].night"/></td>

Now, the scope will be changed, but with the problem, that the objects in my occupancy-array are mirrored. That means by changing occupancy-early in week 1 will change occupancy-early in the other weeks too.

I need help!

4
  • Can you provide JS Fiddle? Commented Nov 8, 2016 at 13:24
  • Yes, wait. I changed structure for working without $index. Commented Nov 8, 2016 at 13:28
  • Here is it: jsfiddle.net/8Lmq8ub5 Commented Nov 8, 2016 at 13:41
  • I didn't get it runnable, because it's my first JS Fiddle, maybe you will get it. Commented Nov 8, 2016 at 13:42

1 Answer 1

0

I think you can try assign object to occupancies and try again.

$scope.occupancies = {};
Sign up to request clarification or add additional context in comments.

1 Comment

The same issue. But I thought about deleting the part of setting the occupancy array, because its unnecessary since I don't use the second ng-repeat. Now it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.