Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I have an attribute with it's data being an object? I want to set the data to two indices:

<div ng-repeat="shift in schedule.shifts">
    <div ng-repeat="tasks in shift" data-drag="{'taskIndex' : $index, 'shiftIndex' : $parent.$index}"></div>
</div>

I basically want to set data-drag to contain both the $index and $parent.$index. The code above doesn't work!

Thanks

EDIT

So the problem is not the syntax above. It comes from this other line that I have:

 // Grab the object data
 var dragData = "";
 scope.$watch(attrs.drag, function (newValue) {
      dragData = newValue;
 });

The $watch returns this error:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

If I simply assign data-drag="$index" then, it is all fine. Why is it that I cannot pass an object?

Why?

share|improve this question
    
How doesn't the code work? What are you trying to do with the indexes in data-drag? –  marck 23 hours ago
    
Can you post a plunker and indicate which part of your code doesn't work. –  ryeballar 21 hours ago
    
Using data-drag="{'taskIndex' : $index, 'shiftIndex' : $parent.$index}" returns an error! The syntax is wrong ... It says $digest() aborted after trying for 10 times. –  Kousha 1 hour ago
add comment

1 Answer

Not sure what data-drag is but you can use ng-init to create an object with the inner and outer index in it.

<div ng-repeat="shift in schedule.shifts" ng-init="shiftIndex = $index">
    <div ng-repeat="tasks in shift" 
        ng-init="myVar = {taskIndex: $index, shiftIndex: shiftIndex}" 
        data-drag="myVar">
    </div>
</div>

http://jsfiddle.net/6DuU9/

share|improve this answer
    
Problem is that schedule.shifts and shifts.tasks dynamically change and therefore $index and $parent.$index change as well. If I use ng-init I then need to reassign myVar everytime the arrays change. I would rather if I could pass the indices directly to data-drag –  Kousha 23 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.