[英]How can I access value returned after Angular $resource $save()?
我正在使用PHP CRUD Api服务简单的AngularJS列表应用程序。
当我向数据库中添加新项目时,我想刷新列表以显示新项目。
我的想法是将数据推送到$ save成功回调中的列表数组中。 这很好用,但是我需要知道PHP API编写的记录的“ id”。
如该代码所示,PHP Api确实传递了lastInsertId()
function create($table, $data) {
$fields = $this->filter($table, $data);
$sql = "INSERT INTO " . $table . " (" . implode($fields, ", ") . ") VALUES (:" . implode($fields, ", :") . ");";
$bind = array();
foreach($fields as $field)
$bind[":$field"] = $data[$field];
$result = $this->run($sql, $bind);
return $this->db->lastInsertId();
}
$db = new db();
$rowId = $db->create($table,$data);
$requestContentType = $_SERVER['HTTP_ACCEPT'];
$this ->setHttpHeaders($requestContentType, $statusCode);
$response = $this->encodeJson($rowId);
echo $response;
问题是,我不知道如何使用下面的代码访问成功回调中的lastInsertId()
data.$save()
.then(function (res) {
// push the new data in to the list array here - how do I access the lastInsertId() that is returned from the Api?
})
.catch(function (req) {
// error
})
.finally(function () {
});
这是插入新记录后刷新应用程序的最佳方法,还是我做错了这条路?
以防万一将来有人偶然发现这个问题,问题不在于$save
函数,而是PHP CRUD Api的问题-它返回的是最后插入的ID,但不是JSON编码格式。
一旦更改了php Api以返回ID JSON编码的ID(例如{"id": "185"}
),我就可以访问res.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.