如何从另一个函数调用作用域函数

How to call a scope function from another one angularjs?

本文关键字:作用域 函数 函数调用 另一个      更新时间:2023-09-26

我有一个填满数据的表;我有一个删除一行的按钮,并且我有输入字段,显示来自表中选定行的数据。

首先我必须选择那一行…然后输入字段r,填充来自同一行....的数据完成后,我可以按下删除键…这一行被删除了…被删除行下面的行现在被选中了。(我可以改变它)……但是,已删除行的旧值仍然在输入字段中…

现在我必须做点什么…两个选择:

  1. 将所选行设置为null,并清除所有输入字段…(我不知道怎么做…)

  2. 使用新选择的行并将其数据填充到输入字段中…

所以请帮助我,这里是代码:

在我的html文件中有:

<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>

有一个控制器....用这个:

$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;
};

您可以在删除后更新学生:

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

或者如果你只想更新几个属性,例如firstname:scope.student美元。Firstname = $scope.result[0].firstname;

我认为如果您在post中添加将selectedRow和student设置为null的行,则成功是一个良好的开端。

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

除此之外,我建议您只在selectedRow不为空时显示表单。

像这样:

<div ng-show="selectedRow !== null">
    <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>
</div>