Javascript Angular无法读取属性';车身';未定义的
我遇到了一个错误,我不确定是什么问题 angular.js:14642 TypeError:无法读取未定义的属性“body” 我正在尝试异步更新帖子。我可以异步地添加、获取、删除帖子,只需要为update函数这样做 main.jsJavascript Angular无法读取属性';车身';未定义的,javascript,php,angularjs,laravel,Javascript,Php,Angularjs,Laravel,我遇到了一个错误,我不确定是什么问题 angular.js:14642 TypeError:无法读取未定义的属性“body” 我正在尝试异步更新帖子。我可以异步地添加、获取、删除帖子,只需要为update函数这样做 main.js $scope.updatePost = function(post){ $http.post('/auth/upost/' + post.id, { body: $scope.post.body, }).then(function(
$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>
编辑
更新
正确的代码,我不得不删除$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);
});
};
你需要body:$scope.body,?我需要传递更新后的body内容,你认为我应该怎么做?关于$http.post('/auth/upost/'+post.id,{body:post,})@Sajeetharan我现在知道了angular.js:12701 posthttp://127.0.0.1:8000/auth/upost/36 422(不可处理实体)
我得到了这个{body:[“正文必须是字符串。”]}