AngularJS创建模板的新范围是什么?范围、模板、AngularJS

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

使用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
        }
    }
});
阅读全文

相关推荐

最新文章