我正在尝试在angularJS中执行内联编辑,然后更新在Mysql表中输入的数据,但我是角度新手并且不知道如何处理输入的数据(来自指令)并将其传递给控制器以将其保存在DB中,所以在下面的代码中:变量tid
在编辑后仍然是空的..如何在内联编辑后存储新的id以将它们发送到数据库?
HTML:
<tbody>
<tr ng-repeat="item in ItemsByPage[currentPage]">
<td>
<div contentEditable="true" ng-model="tid" val='tid'>
{{item.id}}
</div>
<button ng-click="inlineupdate()" type="button">
Save
</button>
</td>
</tr>
</tbody>
控制器中的:
$scope.inlineupdate = function () {
var data = {
tid : $scope.val,
};
var request = $http({
method: "post",
url: "inlineupdate.php",
params: {
action: "post"
},
data: data
});
return( request.then( handleSuccess, handleError ) );
};
和指令:
myApp.directive('contenteditable', function() {
return {
restrict: 'E',
scope: {
val : '='
},
link: function(scope, elm, ctrl) {
// view -> model
elm.bind('blur', function() {
scope.$apply(function() {
ctrl.$setViewValue(elm.html());
});
});
// model -> view
ctrl.render = function(value) {
elm.html(value);
};
// load init value from DOM
ctrl.$setViewValue(elm.html());
elm.bind('keydown', function(event) {
console.log("keydown " + event.which);
var esc = event.which == 27,
el = event.target;
if (esc) {
console.log("esc");
ctrl.$setViewValue(elm.html());
el.blur();
event.preventDefault();
}
});
}
};
和inlineupdate.php文件来更新mysql中的数据:
<?php
header('Content-Type: application/json');
include 'connect.php';
$db = new database();
$db->setDb_name('training');
$db->connect();
if(isset($_POST)){
$id = $_POST['val'];
$name = 'name';
$data = $db->inlineupdate('user',array('id'=>$id),array('name'=>$name));
echo json_encode($data);
}
mysql_close();
?>
控制台在inlineupdate.php文件中触发&#34;未定义索引:tid的通知&#34;