Ng-repeat, first item with different directive

43,794

Solution 1

You can use ng-repeat-start and ng-repeat-end as follows:

angular.module('example', [])
  .controller('ctrl', function Ctrl($scope) {
    $scope.items = [1, 2, 3];
  })
  .directive('big', function() {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
        element.css('font-size', '30px');
      }
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="example" ng-controller="ctrl">
  <div ng-repeat-start="item in items" ng-if="$first" big>
    big item {{item}}
  </div>
  <div ng-repeat-end ng-if="!$first">
    item {{item}}
  </div>
</div>

The documentation can be found under ng-repeat.

Solution 2

See this fiddle http://jsfiddle.net/nicolasmoise/xLfmK/2/.

You can create one directive to which you pass a condition. Depending on that condition it will either display the square or the big-square as such.

         <div ng-repeat="repeat in repeater" condition="$first" square></div>

Note If you don't want to alter the directives you're already made, you can always have square be a master directive that calls the other two.

Solution 3

If you don't mind using another <div> inside of your <li>, you should be able to get away with doing conditional blocks of <div> using ng-if="$index == ??".

Maybe something like this:

<div ng-repeat="i in placeholders">
  <div bigsquare class="set-holder {{i.class}}" droppable="{{i.type}}" ng-if="$index == 0">
    ...
  </div>
  <div mediumsquare class="set-holder {{i.class}}" droppable="{{i.type}}" ng-if="$index == 1">
    ...
  </div>
  <div square class="set-holder {{i.class}}" droppable="{{i.type}}" ng-if="$index > 1">
    ...
  </div>
</div>

It's a little more verbose, but it nicely separates out the templates so that you can have them pretty independent of each other.

Solution 4

<!-- Here is a code sample which I used in one of my angularjs ionic apps.  -->
<!-- Regular ng-repeat -->
<!-- ng-if="$first" to determine <input focus-input> or not -->
<ion-item class="item item-input item-stacked-label" ng-repeat="input in template.inputs">
  <label class="input-label bh-dark" for="{{input.id}}">{{input.title}}</label>
  <div class="clearfix">
    <div class="bh-left">
      <input ng-if="$first" focus-input id="{{input.id}}" type="{{input.type}}" placeholder="{{input.choices[0]}}" ng-model="input.answer">
      <input ng-if="!$first" id="{{input.id}}" type="{{input.type}}" placeholder="{{input.choices[0]}}" ng-model="input.answer">
    </div>
    <div class="bh-right">
      <i class="icon ion-ios-close-empty bh-icon-clear" ng-click="clearField(input.id)" ng-show="input.answer"></i>
    </div>
  </div>
</ion-item>
Share:
43,794

Related videos on Youtube

Deepsy
Author by

Deepsy

My name is Viktor Kirilov and I'm living in Montana, BG. I started working on the web about 6 years ago. Since then I have been using many technologies and tools for software developing. I have been participated in different IT competitions and I've achieved top positions. In the past few years I've practiced and tried to master languages such as PHP and JavaScript, HTML and CSS. In my lastest projects I've been using some of the most famous Javascript frameworks/libs such as BackBone, Angular, Require, jQuery. I've created my own PHP MVC framework called AlbaFramework. My experience with databases includes MongoDB and MySQL as well as Redis and Memcached. Since I found NodeJS I'm in love with it and I'm currently running FinalBurnout and Smartly on it. Also I've been in touch with python, java and many more. I've been building simple application with C++, C# and VB. My experience with mobile devices includes Java and Android. I got big experience with Linux based system ( Fedora, Debian, Ubuntu ).

Updated on July 05, 2022

Comments

  • Deepsy
    Deepsy about 2 years

    I got the following code:

    <div ng-repeat="i in placeholders" square class="set-holder {{i.class}}" droppable="{{i.type}}"></div>
    

    How I make the first item has the directive bigsquare, while the others have just square.

    I've tried:

    <div ng-repeat="i in placeholders" {{= $first ? 'big' : ''}}square class="set-holder {{i.class}}" droppable="{{i.type}}"></div>
    

    but sadly I the result is:

    <div ng-repeat="i in placeholders" {{= $first ? 'big' : ''}}square class="set-holder test" droppable="3"></div>
    

    a.k.a. the binding don't get compiled.