【问题标题】:How to call a scope function from another one angularjs?如何从另一个angularjs调用范围函数?
【发布时间】:2016-11-09 23:21:25
【问题描述】:

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

首先我必须选择该行...然后输入字段r 填充来自同一行的数据...一旦我完成它,我可以按删除...该行是已删除...现在选择了该已删除行下方的行...(我可以将其更改为不)...但是已删除行中的旧值仍在输入字段中...

现在我必须做点什么...2 个选项:

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

  2. 使用新选定的行并将其数据填充到输入字段中...witch 在我看来是更好的方法...

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

在我的 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;
};

【问题讨论】:

    标签: javascript angularjs html-table


    【解决方案1】:

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

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

    或者,如果您只想更新一些属性,例如名字: $scope.student.firstname = $scope.result[0].firstname;

    【讨论】:

    • 谢谢你帮了我很多,我投了你的票,但似乎我不能提高你的分数,因为我的声誉很低....这是最终结果:$scope.student = $scope。结果[$scope.selectedRow];
    【解决方案2】:

    我认为,如果您在帖子成功中添加将 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-26
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      相关资源
      最近更新 更多