简体   繁体   English

如何在同一页面上使用2 ng-repeat指令在Angular中隔离/封装$ index?

[英]How can I isolate/encapsulate $index in Angular using 2 ng-repeat directives on the same page?

I have a website built with Angular 1.4 with 2 slideshows. 我有一个使用Angular 1.4构建的网站,并带有2个幻灯片。 One in the header and one for a partner logo slider. 标题中一个,伙伴徽标滑块中的一个。 They are in the same view when all it said and done. 说完所有内容,他们的观点是一致的。

Header

<div layout="row">
<div flex="100" flex-gt-sm="70" hide-xs class="header-slider center">
    <img 
        ng-repeat="i in slides" 
        class="header-slide-animation"
        src="{{i.img}}"  
        ng-hide="!isCurrentSlideIndex($index)" 
        ng-class="{'active':isCurrentSlideIndex($index)}"/>
</div>

And then here's the logo slider 然后是徽标滑块

<div class="partners-slider center">
<a ng-repeat="i in slides" 
    class="slide-animation" 
    href="{{i.href}}" 
    target="_blank" 
    ng-hide="!isCurrentSlideIndex($index)" 
    ng-class="{'active':isCurrentSlideIndex($index)}">
     <img src="{{i.img}}" alt="{{i.title}}" />
</a>

I'm following this tutorial here http://onehungrymind.com/build-sweet-photo-slider-angularjs-animate/ and it uses TweenMax animate the sides. 我在这里跟随本教程http://onehungrymind.com/build-sweet-photo-slider-angularjs-animate/ ,它使用TweenMax为侧面动画。

Everything was working great with one slide show on the page but I have each split into separate directives being included on the same page and the problem is that $index is conflicting. 页面上只有一张幻灯片,一切工作都很好,但是我将每个页面分成了独立的指令,并且问题是$ index冲突。 The partner logo animation moves faster than the header slide show and also has a larger array so you can imagine the problem when the header's $index gets overwritten by the other and is out of range, etc... What can I do to isolate $index so the two slideshows don't step on each other? 伙伴徽标动画的移动速度比标题幻灯片放映的速度快,并且具有更大的数组,因此可以想象当标题的$ index被另一个覆盖并且超出范围时的问题,等等。索引,以便两个幻灯片不会彼此踩在一起?

It's not $index that is the problem, it is the shared scope whereby each directive is sharing the parent controller scope. 问题不是$index ,而是每个指令共享父控制器作用域的共享作用域。

$index is actually isolated as it only exists in each child scope of ng-repeat $index实际上是隔离的,因为它仅存在于ng-repeat每个子范围中

So whatever isCurrentSlideIndex($index) is doing in controller will be shared by both instances 因此, isCurrentSlideIndex($index)在控制器中执行的操作将由两个实例共享

The simple solution is to use an isolated scope in directive so both are separate instances. 一种简单的解决方案是在指令中使用隔离范围,因此两者都是单独的实例。

Just move the methods used in controller to directive. 只需将控制器中使用的方法移至指令即可。 You can then use one attribute in directive to receive the images in the isolated scope from the parent controller 然后,您可以在指令中使用一个属性,以从父控制器接收隔离范围内的图像

Alright I found an article that helped me realize a solution to the problem. 好吧,我找到了一篇文章,帮助我实现了该问题的解决方案。 http://codeutopia.net/blog/2014/11/10/angularjs-best-practices-avoid-using-ng-repeats-index/ http://codeutopia.net/blog/2014/11/10/angularjs-best-practices-avoid-using-ng-repeats-index/

Overall the moral of the story is not to use $index because it can be problematic. 总的来说,这个故事的寓意是不要使用$ index,因为它可能会有问题。 I chose to modify the view to pass the image object and check for that against the current index. 我选择修改视图以传递图像对象,并根据当前索引检查该对象。

HTML HTML

<div layout="row">
<div flex="100" flex-gt-sm="70" hide-xs class="header-slider center">
    <img 
        ng-repeat="j in headerSlides" 
        class="header-slide-animation"
        src="{{j.img}}"  
        ng-hide="!isCurrentHeaderSlide(j)" 
        ng-class="{'active':isCurrentHeaderSlide(j)}"/>
</div>

This is what the JavaScript was 这就是JavaScript

$scope.isCurrentSlideIndex = function (image) {
    return $scope.currentIndex === index;
};

This is what I changed it to 这就是我将其更改为

$scope.isCurrentHeaderSlide = function (slide) {
    return $scope.headerSlides[$scope.currentIndex] == slide;
};

And it works :-) 它有效:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM