What is best way to get angularjs ng-repeat variable to a directive called withn the repeat

 ̄綄美尐妖づ 提交于 2019-12-11 02:32:42

问题


The situation I have is that I want to change a somewhat different element depending on the precise type of the variable in the ng-repeat loop which is not known until runtime. I had thought that the scope in effect would have the variable as an object but when I break in my directive it is not so. If I make an isolate scope in my directive using "@val" for a ng-repeat variable named "val" thus does not work. I tried without an isolated scope but again it was not present when the directive fired. So what should be done in such a case? I am sure I am not the first person that wanted to access an ng-repeat loop variable from a custom directive.

If I try something like then I get only the string "val" where val is actually a non string object within the loop. Is this some weird effect of when different things are evaluated?


回答1:


What you are trying to do should work just fine. Here is an example...

http://jsfiddle.net/HB7LU/4269/

<div ng-repeat="val in values"> <my-directive val="{{val}}">{{val}}</my-directive>

You can see that the value from the repeater is easily binded into the child directive.

Since you didn't provide any code, its hard to see where you are going wrong, but it might have something to do with the fact that you are using a table in your html. There was a bug in Angular, which was fixed in 1.2.13, related to repeating over table elements, since browsers actually manipulate the DOM inside of tables on their own sometimes.

Please see this bug report regarding that issue.

I would update to the latest version of Angular and try again. If you still can't get it working, try to avoid using an html table. You can easily fake a table by styling divs appropriately



来源:https://stackoverflow.com/questions/18220840/what-is-best-way-to-get-angularjs-ng-repeat-variable-to-a-directive-called-withn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!