将值从表传递到angularjs中的文本框

时间:2015-07-28 11:53:46

标签: angularjs data-binding ng-repeat angular-ngmodel

我正在尝试将值从表格传递到angularjs中的文本框。当我点击“传递值”时,该值应该通过。按钮。谁能帮我这个? 我的代码在下面分享:



angular.module('app', [])
  .controller('controller', function($scope) {
    $scope.array = [{
      id:1,
      name: 'name1',
      address: 'address1'
      
    }, {
      id:2,
      name: 'name2',
      address: 'address2'
    }, {
      id:3,
      name: 'name3',
      address: 'address3'
    }];
  
  $scope.edit=function(id){
   
  };
  });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<body ng-app="app" ng-controller="controller" class="container">
  <table class="table table-condensed">
    <tr>
      <th>id</th>
      <th>name</th>
      <th>address</th>
    </tr>
    <tr ng-repeat="item in array" >
      <td>{{item.id}}</td>
      <td>{{item.name}}</td>
      <td>{{item.address}}</td>
      <td><button ng-click="edit(item.id)">pass the value</button></td>
    </tr>
  </table>
  
  <form>
    name <input type="text" ng-model="item.name" />
    address <input type="text" ng-model="item.address"/>
  </form>
</body>
&#13;
&#13;
&#13;

enter image description here

2 个答案:

答案 0 :(得分:2)

不是只传递id,而是将整个项目传递给编辑函数:

<button ng-click="edit(item)">

然后,您可以将范围内的项目设置为传递的项目:

$scope.edit=function(item){
  $scope.item = item;
};

Plunkr

答案 1 :(得分:0)

您应该在点击功能中为变量分配索引。像

<button ng-click=" newIndex = $index; ">pass the value</button>

从数组中获取此索引值。像

name <input type="text" ng-value="array[newIndex].name" />
address <input type="text" ng-value="array[newIndex].address"/>

请务必先在控制器中声明newIndex。像

$scope.newIndex = null;