保存编辑后,ng-model不会更改

时间:2015-12-17 18:55:04

标签: angularjs angular-ngmodel

我尝试用angularjs和php api制作简单的CRUD 问题出在angularjs

我有这样的代码:

    $scope.editData = function(pid) {
    $scope.hideform = false;

    $scope.edit = false;
    $scope.pid = $scope.projects[pid].pid;
    $scope.title = $scope.projects[pid].title;
    $scope.pcode = $scope.projects[pid].pcode;
    $scope.type = $scope.projects[pid].type;
    $scope.proj_type = $scope.projects[pid].proj_type;
    };
$scope.saveData = function(pid, title, pcode, type2, proj_type){
        $http.post("api/getProject.php",
            {type: "edit", id:pid, title:title, pcode:pcode, type2:type2, proj_type:proj_type})
            .success(function(data) {
                notification(data.success, data.message);
                $scope.hideform = true;
        });
    };

并具有编辑数据的格式(单击编辑按钮,然后在其上加载ng-model显示表单编辑

像这样

<form class="form-horizontal" ng-hide="hideform" ng-submit="saveData(pid,title, pcode, type, proj_type)">

<h3 ng-hide="edit">Edit Project:</h3>
<div class="form-group">
<label class="col-sm-2 control-label">PID:</label>
<div class="col-sm-10">
<input type="text" ng-model="pid" ng-disabled="!edit" placeholder="PID">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Title:</label>
<div class="col-sm-10">
<input type="text" ng-model="title" placeholder="Title">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Pcode:</label>
<div class="col-sm-10">
<input type="text" ng-model="pcode" placeholder="Pcode">
</div>
</div>
<button class="btn btn-success" ng-disabled="error || incomplete">
<span class="glyphicon glyphicon-save"></span> Save Changes
</button>
</form>

脚本正在运行,但每次我点击保存编辑然后点击编辑按钮(在表格的每一行上),表格输入上的ng-model仍然像我保存的最后数据一样,但其他输入被更改,例如:我编辑了标题,当点击表格上不同行的编辑按钮时,只是pcode改为不同的行数据,标题仍然像我编辑的最后一个数据一样 对不起,如果我的解释让你们所有人都感到困惑 任何帮助将不胜感激

如果我的解释不清楚,这里是图像 Image for explanation

1 个答案:

答案 0 :(得分:0)

使用

解决了此问题
$scope.selectedProject = $scope.projects[pid];

所以editData函数看起来像这样

$scope.editData = function(pid) {

        $scope.hideform = false;

        $scope.edit = false;
        $scope.pid = $scope.projects[pid].pid;
        $scope.title = $scope.projects[pid].title;
        $scope.pcode = $scope.projects[pid].pcode;
        $scope.type = $scope.projects[pid].type;
        $scope.proj_type = $scope.projects[pid].proj_type;

        $scope.selectedProject = $scope.projects[pid];
    };

和ng-model(在每个输入上) 需要像这样改变

<input type="text" ng-model="selectedProject.pid" ng-disabled="!edit" placeholder="PID">

而不是

<input type="text" ng-model="pid" ng-disabled="!edit" placeholder="PID">

现在每个ng-model更改了每个编辑按钮,点击保存更改按钮后点击:) 谢谢!