ng-model 保存编辑后不变
ng-model not change after save editing
我尝试使用 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>
脚本正在运行,但每次我单击保存编辑然后单击编辑按钮(在 table 的每一行上)时,表单输入上的 ng-model 仍然与我保存的最后数据相同但其他输入已更改,例如:我编辑了标题,当单击 table 上不同行上的编辑按钮时,只是 pcode 更改为不同的行数据,标题仍然与我编辑的最后一个数据相同
对不起,如果我的解释让你们感到困惑
任何帮助将不胜感激
如果我的解释不清楚,这是图片
使用
解决了这个问题
$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 都更改了每个编辑按钮都在单击保存更改按钮后单击:)
谢谢!
我尝试使用 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>
脚本正在运行,但每次我单击保存编辑然后单击编辑按钮(在 table 的每一行上)时,表单输入上的 ng-model 仍然与我保存的最后数据相同但其他输入已更改,例如:我编辑了标题,当单击 table 上不同行上的编辑按钮时,只是 pcode 更改为不同的行数据,标题仍然与我编辑的最后一个数据相同 对不起,如果我的解释让你们感到困惑 任何帮助将不胜感激
如果我的解释不清楚,这是图片
使用
解决了这个问题$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 都更改了每个编辑按钮都在单击保存更改按钮后单击:) 谢谢!