AngularJS创建模板的新范围是什么? [英] AngularJS Create new scope in template?

查看:84
本文介绍了AngularJS创建模板的新范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Symfony2.x,我有一个树枝循环经历的一些数据资料,而且我也有一个NG重复相似的元素回事(差值被这些的获得装入虽然背景),但两者都是共享相同的功能。我有一些奇怪的功能,但将在NG-重复版本可以正常使用细树枝环路版本回事。我有一种感觉,它只是一个范围的问题。

using Symfony2.x, I have a twig loop going through data for some data, and I also have an ng-repeat going on for similar elements (difference being these ones get loaded in the background though), but both are to share the same functionality. I have some odd functionality going on in the twig loop versions that are working perfectly fine in the ng-repeat versions. I have a feeling it's simply a scope issue.

我在NG-重复会自动创建重复元素的新范围文档阅读,当然,这并不用树枝循环发生。

I read in the docs that ng-repeat will automatically create a new scope for the repeated elements, but of course this doesn't happen with a twig loop.

一个人怎么手动和preferably只在模板,每个重复的元素调用一个新的范围是什么?

How does one manually, and preferably exclusively IN the template, invoke a new scope per repeated element?

推荐答案

最简单的方法可能是一个指令添加到每个元素。这可以在模板进行。然后该指令可以请求新的范围(通过范围:真正的范围:{} ),每个重复的元素会与之相关联的新的范围。

The easiest way might be to add a directive to each element. This can be done in the template. The directive can then request new scope (via scope:true or scope:{}) and each repeated element will get a new scope associated with it.

您可以在元素上创建一个指令,如:

You can create a directive on an element like:

<div mydirective></div>

然后在你的code,定义指令:

Then in your code, define the directive:

myApp.directive('mydirective',function(){
    return {
        scope: true,
        link: function(scope, elem, attrs){
            // do some scope / element stuff here
        }
    }
});

这篇关于AngularJS创建模板的新范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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