0

(using angularjs 1.3) Inside an ng-repeat block, I have a custom directive that tries to identify a context based on the current item of the repeat loop, i.e. in the link function I have a line:

var context = $parse(attrs.context);

In the view, I have:

<div ng-repeat="item in items">
  <div my-directive context="{{$index}}"></div>
</div>

$index is just an example, I could also have item.title. The issue here is that each time my directive link function is called, context equals 0 or whatever value related to the first iteration of the loop.

Update:

It appears that I don't need $parse. attrs.context is the value that I need. But still, it is always the same 0 value. Strangely I added this line in the link function:

var interpolation = $interpolate(attrs.context);

Believe it or not, but with this line (wherever it is in the function), each further call to the link function brings the expected value in attrs.context (0, 1, 2, ...).

Nicolas Cadilhac
  • 4,680
  • 5
  • 41
  • 62

1 Answers1

0

You need to provide your directive code as well, but I guess that you have non-isolated scope for your directive, which leads to a context variable rewrite on every iteration of ng-repeat.

Sergey Moiseev
  • 2,953
  • 2
  • 24
  • 28
  • Imagine that the link function is just the line I provided. I have an isolated scope. – Nicolas Cadilhac Mar 30 '15 at 15:11
  • 1
    Actually you don't need link function like that. Here is working implementation: http://plnkr.co/edit/KqU4LEfF3tjzSYXMzRbU?p=preview – Sergey Moiseev Mar 30 '15 at 15:36
  • You're absolutely right, and the '@' binding is what I need in my case. Still, it would be interesting to know why the way I was doing doesn't work and now works when I added the interpolate call. – Nicolas Cadilhac Mar 30 '15 at 15:43
  • 1
    I really like to see your code in plunker to look on that with you. Because I also see that interesting. – Sergey Moiseev Mar 30 '15 at 15:57