Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular ng repeat disable all items except first item

I have a list of items. Each item is in div. i want to show only first item enable except others shows disabled.

AngularJs

angular.module('example', [])
.controller('myCtrl', function Ctrl($scope) {
    $scope.items = [1,2,3];
});

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="example" ng-controller="myCtrl">
   <div ng-repeat="item in items">{{item}}</div>
</div>
like image 544
Nitin Avatar asked Dec 02 '25 13:12

Nitin


2 Answers

simply by using $first attribute:

<div ng-app="example" ng-controller="myCtrl">
     <div ng-repeat="item in items" ng-disabled="!$first">{{item}}</div>
 </div>

beware: ng-disabled works with form elements. see documentation for details.

like image 182
illeb Avatar answered Dec 05 '25 04:12

illeb


you can do this using $first.

 angular.module('example', [])
    .controller('myCtrl', function Ctrl($scope) {
    $scope.items = [1,2,3];
      });
    

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

    <div ng-app="example" ng-controller="myCtrl">
         <div ng-repeat="item in items"><span ng-if="$first">{{item}}</span>
<span ng-if="!$first">do whatever you want with this item {{item}} </span>
</div>
</div>
like image 28
Dinesh Shah Avatar answered Dec 05 '25 04:12

Dinesh Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!