简体   繁体   中英

angularjs - add `$index` into `ng-model`

I have this code:

<ul class="unstyled">
    <li class="card" ng-repeat="user in users">
        <form ng-submit="edit()">
            <input type="text" ng-model="editname"  size="30"placeholder="add name here">
            <input class="btn-primary" type="submit" value="edit">
        </form>
    </li>
</ul>

which card class will be duplicate based on users array. I want to have unique ng-model so that when I edit one of them, it will not affect others. I believe I can use $index . How can I make

ng-model="editname"

into

ng-model="editname + $index"

Or is there a better way to do this?

Try to naming model with ng-repeat instance

Like this

 ng-model="user.editname" 

You can check duplicate on $scope.users list by editname property

If you really care about the index, you can set the index on the user instance. Then use 'user.index' as the model name. If not, you can use the answer by Anik.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM