最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS ng-repeat with delimiter - Stack Overflow

programmeradmin1浏览0评论

trying to build an alphabet menu with delimiter('|') like following

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

am I doing the right thing? is there any better way to repeat items with delimiter in angularJS? Please guide me.

<div ng-app="myApp">
  <div ng-controller="myController as vm">
    <span ng-repeat="item in vm.menuItems">
      <a href="#">{{item}}</a>&nbsp;<span ng-if="!$last">|</span>
    </span>
  </div>
</div> 

<script>
  angular.module('myApp',[])
  .controller('myController',function(){
    var vm =this;
    vm.menuItems=[];
    activate();
    function activate(){
     vm.menuItems = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
    }
  });
</script>

Plunker link :

trying to build an alphabet menu with delimiter('|') like following

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

am I doing the right thing? is there any better way to repeat items with delimiter in angularJS? Please guide me.

<div ng-app="myApp">
  <div ng-controller="myController as vm">
    <span ng-repeat="item in vm.menuItems">
      <a href="#">{{item}}</a>&nbsp;<span ng-if="!$last">|</span>
    </span>
  </div>
</div> 

<script>
  angular.module('myApp',[])
  .controller('myController',function(){
    var vm =this;
    vm.menuItems=[];
    activate();
    function activate(){
     vm.menuItems = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
    }
  });
</script>

Plunker link :http://plnkr.co/edit/FJEi8T36KGIx1K0hHrFG

Share Improve this question asked Jun 28, 2015 at 20:23 BeingDevBeingDev 4262 gold badges8 silver badges24 bronze badges 3
  • Don't use &nbsp; for spacing, add a class to the span and style it to create the spacing required, it will be easier to have it the same and mean the line will not finish with a space which can create issue when trying to align it in the middle. Otherwise $last usage seems correct to me here for this scenario. – GillesC Commented Jun 28, 2015 at 20:32
  • @gillesc thanks for feedback, will avoid using &nbsp; – BeingDev Commented Jun 28, 2015 at 20:34
  • @gillesc it's called "non-breaking" for a reason. Maybe it's a desired quality in this case. So you rather want to say "Don't use &nbsp; unless ...". – a better oliver Commented Jun 28, 2015 at 20:58
Add a ment  | 

2 Answers 2

Reset to default 5

That's not a terrible solution at all! A "cleaner" approach, though, could be to do this with css:

ul.menu li {
  display: inline;
}

.menu li:not(:last-child):after {
   content: " |";
}

And I've changed your template to be

<div ng-app="myApp">
  <ul class="menu" ng-controller="myController as vm">
    <li ng-repeat="item in vm.menuItems">
      <a href="#">{{item}}</a>
    </li>
  </ul>
</div>

It's really up to you as to whether the vertical bar should be in the markup (does it add semantic value?) or in the style (is it a purely visual alteration?).

http://plnkr.co/edit/kwGWBZfeWnuQ2wb4b9Ev?p=preview

I prefer using ng-repeat-start and ng-repeat-end when I need delimiters. That way you are not forced to have an element surrounding both your item and your delimiter. Use an ng-if to remove the last delimiter as you were before. Note that I moved the &nbsp; inside the delimiter so that there is not an &nbsp; trailing your Z (as with yours).

<div ng-controller="myController as vm">
   <a ng-repeat-start="item in vm.menuItems" href="#">{{item}}</a><span ng-repeat-end ng-if="!$last">&nbsp;| </span>
</div>

http://plnkr.co/edit/P3E6c6JkcVAH8Z4IBGme?p=preview

发布评论

评论列表(0)

  1. 暂无评论