newb newb - 28 days ago 7
AngularJS Question

How to call a scope function from another one angularjs?

I have a table filled with data;
I have a button to delete a row,
and I have input fields that show data from the selected row from table.

First I have to select that row...then the input fields

r
filled with the data from that same row....once I've done it, I can press delete...and the row is deleted...and the row below that deleted row is now selected...(which I can change not to be)...but the old values from the deleted row are still in the input fields...

Now I have to do something...2 options:


  1. set the selected row to null, and clear all the input fields...(which I have no idea how to do it...)

  2. use the new selected row and fill its data into the input fields...witch is in my opinion the better way to go...



So please help me, here is the code:

In my html file I have this:

<p>ID</p>
<input ng-model="student.id">
<p>Name</p>
<input ng-model="student.firstname">
<p>Last Name</p>
<input ng-model="student.lastname">

<button ng-click="deleteStudent(student)">Delete Student</button>


<table name="tableStud" arrow-selector>
<tbody>
<tr>

<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>From</td>
</tr>

<tr ng-repeat="student in result"
ng-class="{'selected':$index == selectedRow}"
ng-click="setSelected(student,$index)">
<td>{{ student.id }}</td>
<td>{{ student.firstname }}</td>
<td>{{ student.lastname }}</td>
<td>{{ student.mestorodjenja.ime }}</td>

</tr>

</tbody>
</table>


I have a controler....with this:

$scope.deleteStudent = function(student) {
$http.post('http://localhost:8080/CreditCardWEB/rest/cc/delete',
student).success(function(data) {
$scope.result.splice($scope.selectedRow, 1);

});

};

$scope.selectedRow = null;

$scope.setSelected = function(student, index) {
$scope.student = student;
$scope.selectedRow = index;
};

Answer

You could update the student after delete:

$scope.result.splice($scope.selectedRow, 1);
$scope.student = $scope.result[0]; 

Or if you want update just a few attributes, for example firstname: $scope.student.firstname = $scope.result[0].firstname;