0

I am trying to pass the value from table to text box in angularjs. value should be pass when i click 'pass the value' button. can anyone help me with this? my code is shared below:

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>

enter image description here

2
  • Can you show your code rather than screen shot ? Commented Jul 28, 2015 at 11:55
  • yeah, i shared my code above Commented Jul 28, 2015 at 11:59

2 Answers 2

2

Instead of just passing the id, pass the whole item to the edit function:

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

Then you can set the item on scope to the passed item:

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

Plunkr

Sign up to request clarification or add additional context in comments.

Comments

0

You should assign your index in a variable in click function. Like

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

And fetch this indexed value from array. Like

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

Make sure to declare newIndex in your controller first. Like

$scope.newIndex = null;

1 Comment

doesn't seems working for me. can you share the working code for me?

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.