填充输入字段angularJs

时间:2015-10-30 03:38:06

标签: angularjs

我有下表

   <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>

当点击编辑人物时,我调用一个传递该行的那个人对象的函数,然后我想填充三个额外的输入字段,如下所示:

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

我的功能如下:

   $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;
};

然而我在控制台中得到了对象,但是我抛出了一个错误:

Error: person[0] is undefined

然后我将其更改为以下内容:

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

得到         错误:$ scope.person未定义

1 个答案:

答案 0 :(得分:7)

试试这个:

$scope.person = {};

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