简体   繁体   中英

How to get each row data of a table using Angular.js

I need one help.i want to fetch each row data of a table using Angular.js.I am explaining my code below.

    <tr ng-repeat="d in days">
    <td>{{d.day_name}}</td>
    <td> <select class="form-control" name="catagory[$index]"  id="catagory[$index]" ng-model="catagory" ng-options="cat.name for cat in listOfCatagory track by cat.value " ng-change="removeBorder('catagory',$index,catagory.value);" >
    </select></td>
    <td>
    <select class="form-control" name="subcatagory[$index]"  id="subcatagory[$index]" ng-model="subcatagory[$index]" ng-options="sub.name for sub in listOfSubCatagory[$index] track by sub.value " ng-change="setSubCatagory($index,subcatagory[$index].value);" >
    <option value="">Select Subcategory</option>
    </select>

    </td>
     <td><input type="text" name="comment[$index]" id="comment" class="form-control oditek-form" placeholder="Add Comment" ng-model="comment[$index]" ng-keyup="comment($index,comment[$index]);"></td>
    </tr>   
<input type="button" class="btn btn-success" ng-click="saveResturantDetails(billdata);"  id="saveData" value="Save"   style="margin-right:20px;"/>

When user will click on save button the each row data should fetch to the controller.I am explaining my code below.

$scope.saveResturantDetails=function(billdata){
        for(var i=0;i< $scope.days.length;i++){
            var data={'cat':$scope.catagory[i].value,'subcat':$scope.subcatagory[i].value,'comment':$scope.comment[i]};
            arr.push(data);
        }
        console.log('arr',arr);
}

Here i am using $index in each model so that i can get data into a loop.But in this way i am getting this following error.

TypeError: Cannot read property 'value' of undefined 

Please help me to resolve this issue.

In your controller Declare a scope object called answers.

$scope.days = [{name:"m"},{name:"t"}];
$scope.answers={};
$scope.save = function(){ console.log($scope.answers)};

Then your html iterate days and assign ng-model to answers object.

<div><tr ng-repeat="d in days">
<td>{{d.name}}</td>
<td><input type="text" ng-model="answers['comment'+$index]"/></td>
</tr></div>
<button ng-click="save()">Save</button>

Ng Repeat creates Chid Scope so we won't get that in controller. This will definitely work. I Checked

Sorry for my bad english

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM