admin管理员组

文章数量:1289557

I have a list of disabled input field created via ng-repeat Plunker

<ul>
  <li ng-repeat="name in names">
   <input type="text" disabled value="{{name.item}}"/>
  </li>
</ul>

How can I make the first input field not disabled ? Thanks in advance

I have a list of disabled input field created via ng-repeat Plunker

<ul>
  <li ng-repeat="name in names">
   <input type="text" disabled value="{{name.item}}"/>
  </li>
</ul>

How can I make the first input field not disabled ? Thanks in advance

Share Improve this question edited Apr 7, 2015 at 20:54 Martin Thorsen Ranang 2,4151 gold badge29 silver badges43 bronze badges asked Apr 7, 2015 at 20:12 RaihanRaihan 4,0113 gold badges25 silver badges41 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can use ng-disabled and check the ng-repeat directive's $first variable:

<ul>
  <li ng-repeat="name in names">
    <input type="text" ng-disabled="!$first" value="{{name.item}}"/>
  </li>
</ul>

You could similarly check for $middle, $last, $even, and $odd. If you use $index, you can check for arbitrary row indexes.

Since ng-repeat creates an isolated scope, in the case of a nested ng-repeat, if you want only the first item in the first parent to be enabled, the relevant code would look like:

<div ng-repeat="item in items">
  <ul>
    <li ng-repeat="name in names">
      <input type="text" ng-disabled="!$parent.$first || !$first" value="{{name.item}}"/>
    </li>
  </ul>
</div>

where you access the outer scope's $first (or $index) variable through the inner ng-repeat's $parent variable. I created a fork of your Plunker that shows how this can be done.

You are looking for $index. So, just check if the current $index is not the first "0th":

<ul>
  <li ng-repeat="name in names">
   <input type="text" ng-disabled="$index != 0" value="{{name.item}}"/>
  </li>
</ul>

This is what you're looking for!

<ul>
  <li ng-repeat="name in names">
    <input type="text" ng-disabled="!$first" value="{{name.item}}"/>
  </li>
</ul>

You can see the documentation for ng-repeat for more special variables like $first.

本文标签: javascriptAccess the first item from ngrepeatStack Overflow