2

I have the following table

   <table>
        <tr>
            <td>Id</td>
            <td>Name</td>
            <td>Lastname</td>
            <td>Delete</td>
        </tr>
        <tr ng-repeat="p in ListPeople">
            <td>{{p.id}}</td>
            <td>{{p.name}}</td>
            <td>{{p.lastname}}</td>
            <td>
                <button ng-click="deletePerson(p)">Delete</button>
            </td>
            <td>
                <button ng-click="editPerson(p)">Edit</button>
            </td>
        </tr>
    </table>

When Edit Person is clicked, I call a function which passes in that person object for that row, I then want to populate three additional input fields which are as shown here:

     <input ng-model="person.id"/><br/>
    <input ng-model="person.name" /><br />
    <input ng-model="person.lastname" /><br />

My function is as followed:

   $scope.editPerson = function (person) {

    console.log(person);

    $scope.person.id = person[0].id;
    $scope.person.name = person[1].name;
    $scope.person.lastname = person[2].lastname;
};

Yet I get the object in the console, but I throws an error which is :

Error: person[0] is undefined

I then changed it to the following:

    $scope.person.id = person.id;
    $scope.person.name = person.name;
    $scope.person.lastname = person.lastname;

And get Error: $scope.person is undefined

1 Answer 1

7

Try this:

$scope.person = {};

$scope.person.id = person.id;
$scope.person.name = person.name;
$scope.person.lastname = person.lastname;
Sign up to request clarification or add additional context in comments.

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.