2015-10-30 84 views
1

我下面的表格有填入輸入框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是未定義

回答

6

試試這個:

$scope.person = {}; 

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

謝謝,它的工作。將很快標記爲答案。 –