Code Ratchet
Code Ratchet

Reputation: 6029

populate input fields angularJs

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

Upvotes: 2

Views: 4219

Answers (1)

Rodmentou
Rodmentou

Reputation: 1640

Try this:

$scope.person = {};

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

Upvotes: 7

Related Questions