角度不能读取未定义的属性“body”

时间:2022-08-24 13:16:27

I'm getting an error, and im not sure what the problem is

我得到一个错误,我不确定是什么问题

angular.js:14642 TypeError: Cannot read property 'body' of undefined

角。无法读取未定义的属性“body”

I'm trying to update a post asynchronously. I can add, get, delete posts asynchronously just need to do it for the update function.

我正在异步更新一个帖子。我可以异步地添加、获取、删除帖子,只需要为update函数做这些。

main.js

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

路线

Route::post('auth/upost/{post}', 'PostController@update')->name('update.post');

路线::post(身份验证/ upost / {职位},PostController@update)- >名称(“update.post”);

PostController.php

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

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>

1 个解决方案

#1


0  

correct code, i had to remove the $scope, not sure why but it works.

正确的代码,我必须删除$范围,不知道为什么,但是它可以工作。

Main.js

Main.js

$scope.updatePost = function(post){

    $http.post('/auth/upost/' + post.id, {
        body: post.body

    }).then(function(data, status, headers, config){
        console.log(data);  


    });




};

#1


0  

correct code, i had to remove the $scope, not sure why but it works.

正确的代码,我必须删除$范围,不知道为什么,但是它可以工作。

Main.js

Main.js

$scope.updatePost = function(post){

    $http.post('/auth/upost/' + post.id, {
        body: post.body

    }).then(function(data, status, headers, config){
        console.log(data);  


    });




};