将 angularjs ng-repeat 变量设置为调用 withn the repeat 的指令的最佳方法是什么 [英] What is best way to get angularjs ng-repeat variable to a directive called withn the repeat

查看:18
本文介绍了将 angularjs ng-repeat 变量设置为调用 withn the repeat 的指令的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我想根据 ng-repeat 循环中变量的精确类型更改一个稍微不同的元素,直到运行时才知道.我原以为有效的范围会将变量作为对象,但是当我中断指令时,情况并非如此.如果我在我的指令中使用@val"为名为val"的 ng-repeat 变量创建一个隔离范围,因此不起作用.我在没有隔离范围的情况下尝试过,但是当指令触发时它又不存在.那么在这种情况下应该怎么做呢?我确信我不是第一个想要从自定义指令访问 ng-repeat 循环变量的人.

如果我尝试类似然后我只得到字符串val",其中 val 实际上是循环中的非字符串对象.这是评估不同事物时的某种奇怪效果吗?

解决方案

您尝试执行的操作应该可以正常工作.这是一个例子...

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

<my-directive val="{{val}}">{{val}}</my-directive>

您可以看到来自转发器的值很容易绑定到子指令中.

由于您没有提供任何代码,因此很难看出您哪里出错了,但这可能与您在 html 中使用表格这一事实有关.Angular 中存在一个错误,该错误在 1.2.13 中得到修复,与重复表格元素有关,因为浏览器有时会自己操作表格内部的 DOM.

请参阅关于该问题的错误报告.

我会更新到最新版本的 Angular,然后再试一次.如果您仍然无法使其正常工作,请尽量避免使用 html 表.您可以通过适当地设置 div 样式来轻松伪造表格

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?

解决方案

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

这篇关于将 angularjs ng-repeat 变量设置为调用 withn the repeat 的指令的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆