我收到错误,我不确定问题是什么
angular.js:14642 TypeError:无法读取未定义的属性“body”
我正在尝试异步更新帖子。我可以异步添加,获取,删除帖子,只需要为更新功能执行此操作。
main.js
$scope.updatePost = function(post){
$http.post('/auth/upost/' + post.id, {
body: $scope.post.body,
}).then(function(data){
console.log(data);
$scope.myposts.push(data.data);
});
};
路线
Route::post('auth/upost/{post}', 'PostController@update')->name('update.post');
PostController.php
public function update(Post $post)
{
//
$data = request()->validate([
'body' => 'required|string'
]);
$post->update($data);
$response = new Response(json_encode($post));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
HTML
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts ">
<div id="eli-style-heading" class="panel-heading"><% post.user.name %></div>
<div class="panel-body panel">
<figure>
<p ng-model="post.body" editable-text="post.body" e-form="textBtnForm"> <% post.body %></p>
<p> <% post.created_at %></p>
</figure>
<span>
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)" ng-if="post.deletable"></i>
<button class="btn btn-default" ng-click="textBtnForm.$show()" ng-hide="textBtnForm.$visible">
edit
</button>
<span><button type="submit" class="btn btn-primary" ng-click="updatePost(post)">Update</button></span>
</span>
</div>
</div>
答案 0 :(得分:0)
正确的代码,我必须删除$ scope,不知道为什么,但它的工作原理。
<强> Main.js 强>
$scope.updatePost = function(post){
$http.post('/auth/upost/' + post.id, {
body: post.body
}).then(function(data, status, headers, config){
console.log(data);
});
};