javascript - 如何将编辑后的数据提交到AngularJS中的对象?

标签 javascript angularjs

当我按下“接受”按钮时,任务应该用我在输入中写入的新值进行更新。遗憾的是,它没有像我想要的那样工作。我需要将 id 添加到包含对象的数组中吗?

或者也许有一些好的方法可以将对象发送到函数。代码如下:

<tbody>
    <tr ng-repeat="task in tasks">
        <td>
            <button class="btn btn-danger" ng-click="deleteTask(task)">Delete</button>
            <!--  $index-->
        </td>
        <td>{{task.taskName}}</td>
        <td>
            <input type="checkbox" ng-model="statusCheck"> </td>
        <td style="{{setStyleToTd(statusCheck)}}">{{statusChecker(statusCheck)}}</td>
        <td>
            <button class="btn btn-primary" ng-click="editTask(task)">Edit</button>
        </td>
    </tr>
</tbody>
</table>
</div>
<div class="col-md-offset-8 edit-box" ng-show="editBoxShow">
    <form action="" class="form-inline">
        <div class="form-group">
            <lable>Edit task here:</lable>
            <div class="input-group">
                <input type="text" class="form-control" ng-model="editTaskInput"> </div>
            <button type="button" class="btn btn-success" ng-click="acceptEdit()">Accept</button>
        </div>
    </form>
</div> 

$scope.editTask = function(taskToEdit) {
    $scope.editBoxShow = true;
    $scope.editTaskInput = taskToEdit.taskName;
}
$scope.acceptEdit = function() {
    $scope.editBoxShow = false;
    $scope.taskToEdit = $scope.editTaskInput;
}

enter image description here

最佳答案

您可以在单击编辑按钮时捕获表数据的索引,然后在acceptEdit函数中使用该索引来更新表数据。

$scope.editTask = function (taskToEdit) {
    $scope.selectedIndex=$scope.tasks.indexOf(taskToEdit);
    $scope.editBoxShow = true;
    $scope.editTaskInput = taskToEdit.taskName;
}

 $scope.acceptEdit = function () {
    $scope.editBoxShow = false;
    $scope.tasks[$scope.selectedIndex].taskName=$scope.editTaskInput;
}

关于javascript - 如何将编辑后的数据提交到AngularJS中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45838176/

相关文章:

javascript - 如何在 Javascript 中将 srcElement 转换为字符串

javascript - 在 AngularJS 工厂中的对象内调用函数

javascript - 将文本动态添加到相同的 ID

javascript - ionic Angular : dynamic styling on cards

javascript - 使用请求库的 Firebase 函数未触发

javascript - 使用 oembed 嵌入 youtube 视频

AngularJS - 如何覆盖指令 ng Click

javascript - 从对象数组返回单个记录

javascript - 服务的 Angular 解决 promise

javascript - 在 Controller 之间共享数据,AngularJS