javascript - 你如何以 Angular 传递帖子ID?

标签 javascript angularjs laravel

如果你使用 Laravel,如何以 Angular 的形式传递帖子 ID?

这是我目前拥有的,我尝试引用这个

https://docs.angularjs.org/api/ng/service/ $http

但我不太明白。

Main.js

$scope.like = function() {
    var post = {
        // this doesn't work and i dont know how to pull in the post id.
        id: "<% $post->id %>"
    };
    $http.post('post/like/'+ post).success(function(result) {
        checkLike();
    });
};
function checkLike(){
    var post = {
        id: "<% $post->id %>"
    };
    $http.get('post/'+ post '/islikedbyme').success(function(result) {
        if (result == 'true') {
            $scope.like_btn_text = "Delete Like";
        } else {
            $scope.like_btn_text = "Like";
        }
    });
};

路线

Route::get('post/{id}/islikedbyme', 'PostController@isLikedByMe');
Route::post('post/like', 'PostController@like');

Controller

public function isLikedByMe($id)
{
    $post = Post::findOrFail($id)->first();
    if (Like::whereUserId(Auth::id())->wherePostId($post->id)->exists()){
        return 'true';
    }
    return 'false';
}

public function like(Post $post)
{
    $existing_like = Like::withTrashed()->wherePostId($post->id)->whereUserId(Auth::id())->first();

    if (is_null($existing_like)) {
        Like::create([
            'post_id' => $post->id,
            'user_id' => Auth::id()
        ]);
    } else {
        if (is_null($existing_like->deleted_at)) {
            $existing_like->delete();
        } else {
            $existing_like->restore();
        }
    }
}

最佳答案

这项工作我通过使用 ng-init 传入帖子 id 来获取帖子 id。

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>       // i need someway to pull in the post id. so i used ng-init not sure if this best practices.
        <div class="panel-body panel" ng-init="getL(post)">  


        <i style="color:tomato; float:right; font-size:24px;" ng-click="like(post)" class="glyphicon glyphicon-heart"></i>


            <figure>
                <p ng-model="post.body" editable-text="post.body" e-form="textBtnForm"> <% post.body %></p>
                <p name="created_at" ng-model="post.created_at">   <% post.user.created_at | phpDate : "human" %></p>
            </figure>
            <span>

             <i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)" ng-if="post.deletable"></i>


                  <button ng-if="post.update" class="btn btn-default" ng-click="textBtnForm.$show()" ng-hide="textBtnForm.$visible">
                    Edit
                  </button>

                <span><button ng-if="post.update" type="submit" class="btn btn-primary" ng-click="updatePost(post)">Update</button></span>
            </span>
        </div>
    </div>

Main.js

$scope.like = function(post) {
    $http.post('/post/like/'+ post.id).then(function(result) {
        getL();
    });
};
$scope.getL = function(post){

    $http.get('/post/'+ post.id +'/islikedbyme').then(function(result) {
        if (result == 'true') {
            $scope.like_btn_text = "Delete Like";
        } else {
            $scope.like_btn_text = "Like";
        }
    });
}

路线

Route::get('post/{id}/islikedbyme', 'PostController@isLikedByMe');
Route::post('post/like/{post}', 'PostController@like');

后置 Controller

public function like(Post $post, Request $request)
{
    $existing_like = Like::withTrashed()->wherePostId($post->id)->whereUserId(auth()->id())->first();

    if (is_null($existing_like)) {
        Like::create([
            'post_id' => $post->id,
            'user_id' => auth()->user()->id
        ]);


    } else {
        if (is_null($existing_like->deleted_at)) {
            $existing_like->delete();
        } else {
            $existing_like->restore();
        }
    }
}

关于javascript - 你如何以 Angular 传递帖子ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47505557/

相关文章:

php - Artisan 迁移期间 Laravel 数据库错误

javascript - PHP - 如何将 RGB 颜色转换为 CIE 1931 颜色规范

javascript - React/Redux 传递状态变化 <Picker>

javascript - 在开始日期和结束日期之间添加一行

javascript - 迭代 json 并找到特定的键

php - Laravel 使用 migrate 添加新表

javascript - react : TypeError: Cannot read property 'propTypes' of undefined

javascript - 如何在具有 X-Frame-Options : SAMEORIGIN 的 iframe 中打开外部 url

javascript - AngularJS:不使用单页时有没有办法使用 $routeParams

php - 如何检索 Eloquent 模型,相关模型中的字段等于 "xx"?