Directive inside ng-repeat

3,449 views
Skip to first unread message

David Tejnora

unread,
Mar 29, 2012, 8:46:15 AM3/29/12
to AngularJS
I've tried to write a component using a directive with a template, but
when i put my directive into the ng-repeat directive then it is not
working correctly. When I click on the button then setter function is
called, but the value is not updated.

Here is a fiddle : http://jsfiddle.net/tejnora/A82EV/7/


Second issue:
Why is it repeated elevens when data array has only two items.

http://jsfiddle.net/tejnora/Z9WJw/

Thank you for your answer

David Tejnora

Peter Bacon Darwin

unread,
Mar 29, 2012, 9:23:13 AM3/29/12
to ang...@googlegroups.com
First issue
You had a couple of problems here:
First since you are binding to a click event outside of angular you need to use $apply to ensure that the bindings get updated.  Alternatively you could try using ng:click instead.
Second the ng-repeat was making a copy of the primitive strings (i.e. the "res" value) so changing the string inside the repeater has no effect on the scope outside.  I changed the scope to pass in objects instead and it all now works.


Pete


David Tejnora

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Krzysztof Danielewicz

unread,
Mar 29, 2012, 9:26:08 AM3/29/12
to ang...@googlegroups.com
Second issue:
Why is it repeated elevens when data array has only two items.

http://jsfiddle.net/tejnora/Z9WJw/

I can help about the second problem. 

First of all,  <div ng-repeat="res in resources">{{res}}<ng-file-chooser file-src="{{resources}}/></div>, remove the interpolate {{ }} from the attribute "file-src", it is used only in 'bind' scope. Second, you want bind to res not to resource, am I rigth?

template'<input type="text" ng:model="fileSrc()"/>',  

Another problem with your directive is with "fileSrc", it is function, and it is not assignable with operator '=' so ng-model, cannot use it. 

I hope that there will be or is a way to use in template two way bound isolated scope variable.




Peter Bacon Darwin

unread,
Mar 29, 2012, 9:27:52 AM3/29/12
to ang...@googlegroups.com
Second issue:
Your file-src binding was incorrect: you had put "{{resource}}" rather than "res" for the value of the file-src field.

It is always good to check the javascript console since there were plenty of errors in there to point the way.

Pete

Peter Bacon Darwin

unread,
Mar 29, 2012, 9:32:57 AM3/29/12
to ang...@googlegroups.com
Hi Krzysztof,
The compiler does manage to update the outer scope OK.  See my fiddle: http://jsfiddle.net/A82EV/10/

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/QCAipdl0MDsJ.

Krzysztof Danielewicz

unread,
Mar 29, 2012, 12:24:48 PM3/29/12
to ang...@googlegroups.com
The compiler does manage to update the outer scope OK.  See my fiddle: http://jsfiddle.net/A82EV/10/

It's only one way update. Of course you can modify the model value, and you get the view value updated. However, change the other way round. Type something in some input text. What you get is not updated model. 
After such a update, angular complains on console.

Error: Expression 'fileSrc()' not assignable. at Error (unknown source) 

    

Peter Bacon Darwin

unread,
Mar 29, 2012, 1:25:32 PM3/29/12
to ang...@googlegroups.com

Ah right. I guess an alternative would be to manually copy the value into the scope and map changes back through a watch but it would be ideal if angular could do this for us.

...from my mobile.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/yhwvLiI_82oJ.

Krzysztof Danielewicz

unread,
Mar 29, 2012, 1:31:53 PM3/29/12
to ang...@googlegroups.com
:) very nice walkaround.

Igor Minar

unread,
Mar 29, 2012, 3:05:21 PM3/29/12
to ang...@googlegroups.com
there is an RFE open to make this better: https://github.com/angular/angular.js/issues/768

On Thu, Mar 29, 2012 at 10:31 AM, Krzysztof Danielewicz <krzysztof....@gmail.com> wrote:
:) very nice walkaround.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.

Boris Letocha

unread,
Mar 29, 2012, 6:40:20 PM3/29/12
to ang...@googlegroups.com
Hi all,

I made it more "angular" style, what do you think about usage of ng-model? Will this work in future or ngModelControler.$render is implementation detail?

I used Peter's workaround with repeated item to be object.

Igor Minar

unread,
Apr 4, 2012, 12:31:51 PM4/4/12
to ang...@googlegroups.com
That's a sweet trick. It relies on the public form control api, which is a legitimate api usage.

/i

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/hMq_8Wd9jkAJ.
Reply all
Reply to author
Forward
0 new messages