AngularJS only shows last Element in ng-repeat

家住魔仙堡 提交于 2019-12-06 01:29:23

Well I've got some good news and some bad news...

Good News You are doing nothing wrong in your code

Bad News You are doing nothing wrong in your code

Explanation The directive you're trying to use FlexSlider has some limitations as currently written. The reason you're only seeing one set of data rendered is because the association of the slide attribute and all other logic within the directive is written into the compile (see line 11 in flex-slider.js) function when anything related to the scope of the directive should be performed within the link function.

Options

  1. Contact the developer of the directive and submit a bug report
  2. Resolve the issue yourself within the directive
  3. Write your own component

Here is a plnkr I created This plnkr shows an ng-repeat that I added to illustrate that you're code should be working based upon how the directive was created.

Note The developer likely never came across this scenario as the intention for creating it may have been to only have one instance at a time.

Thank you all for bringing up this problem and analyze it! It was indeed an angular-flexslider's directive issue. It has been addressed in http://github.com/thenikso/angular-flexslider/issues/20

General information for users who begin with AngularJs :

Some times you use dropdown with ng-repeat

<a href="#" class="dropdown-toggle" data-toggle="dropdown">Language</a>
<ul class="dropdown-menu" ng-repeat="language in languages">
    <li>
        <a href="#">{{ language.name | translate }}</a>
    </li>
</ul>

Don't forget that ng-repeat will repeat the ul too. The output is :

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Language</a>
    <ul class="dropdown-menu ng-scope" role="menu" ng-repeat="language in languages">
        <li><a href="#" ng-click="changeLanguage(language.abv)" class="ng-binding">French</a></li>
    </ul>
    <ul class="dropdown-menu ng-scope" role="menu" ng-repeat="language in languages">
        <li><a href="#" ng-click="changeLanguage(language.abv)" class="ng-binding">English</a></li>
    </ul>
</li>

We can see two ul with a dropdown, this is bad, this is why you can see only last element of the arrays. This is the correct way :

<a href="#" class="dropdown-toggle" data-toggle="dropdown">Language</a>
<ul class="dropdown-menu">
    <li ng-repeat="language in languages">
        <a href="#">{{ language.name | translate }}</a>
    </li>
</ul>

Hope it help !

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