0

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?

1
  • How about user.name? Commented Oct 14, 2015 at 2:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

It works! But I have another problem. How can I now pass this edited ng-model to update in the array?
pass user to edit like this ng-submit="edit(user)"
but already this will be updated with change in model as this model belong to the list
You are right. It just work magically without another additional code.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.