从角度js和数据库列表中删除项目

时间:2014-10-04 04:52:51

标签: mysql angularjs codeigniter

嗨,我是棱角分明的新手。在学习阶段。我在json编码后从数据库中获取了数据。它成功地运作了。我正在使用代码点火器。现在我想删除同时从角度js和db获取的列表。但我无法获得任何教程,或者我无法理解这个概念。如果有人可以帮助我会很棒。

实际上我的逻辑是捕获帖子的id并将此id传递给ci控制器并通过将其发送到模型来根据id删除帖子。从db中删除后再次获取数据,以便获得新结果。我的逻辑是正确的还是有其他有效的方法来执行相同的任务。我无法将post id传递给ci控制器并捕获ci控制器。

app.js

 function PostsCtrlAjax($scope, $http) {
     $http({method: 'POST', 
            url: '<?php echo base_url().'index.php/cms/get_feedback'; ?>'     
     }).success(function(data) {
         $scope.posts = data; // response data 
     });

     $scope.deleteFeed = function (pId) { 
         //Defining $http service for deleting a list 
         alert(pId); //passing post id
         $http({ method: 'DELETE',
                 url: '<?php echo base_url().'index.php/cms/del_feedback'; ?>',
                 data : pId     //how can i send this pId to ci controller
         }).success(function (data) {
               $scope.posts = data; // response data 
         });
     }
  }

HTML

<ul class="list-group">
    <li class="list-group-item feed" ng-repeat="post in posts">                       
        <span style="font-weight: bold">{{post.name}}</span>
        <span style="font-size: 12px;">{{post.email}}</span>
        <span style="font-size: 12px;">{{post.short_date}}</span>
        <br>
        <span style="font-size: 12px;color: #aaa;">                            
            {{post.message_summary}}
        </span>
        <span class="glyphicon glyphicon-remove del_feedback" ng-click="deleteFeed(post.id)" style="position:absolute;top: 0%;right: 0%;color: #E13300;font-size: 10px;display: none;cursor: pointer;"></span>
     </li>
</ul>

1 个答案:

答案 0 :(得分:0)

您使用查询字符串将id发送到ci控制器。

角度js函数

 function PostsCtrlAjax($scope, $http)
 {
 $http({method: 'POST', url: '<?php echo base_url().'index.php/cms/get_feedback'; ?>'}).success(function(data)
 {
 $scope.posts = data; // response data 
 });


 $scope.deleteFeed = function (pId) { 
 //Defining $http service for deleting a person 
 // alert(pId);
 $http({ method: 'DELETE',
     url: '<?php echo base_url().'index.php/cms/del_feedback?id='; ?>' + pId 
 }).success(function (data) 
 {

 $http({method: 'POST', url: '<?php echo base_url().'index.php/cms/get_feedback'; ?>'}).success(function(data)
 {
 $scope.posts = data; // response data 
 });

 });
 }
 }
 //    feedback cont

HTML

   <div class="panel-body" id="ng-app" ng-app ng-controller="PostsCtrlAjax" style="height: 300px;overflow-y: scroll;">


            <ul class="list-group">
                <li class="list-group-item feed" ng-repeat="post in posts">

                    <span style="font-weight: bold">{{post.name}}</span>
                    <!--<span style="font-size: 12px;">{{post.email}}</span>-->
                    <span style="font-size: 12px;" class="pull-right">{{post.short_date}}</span>
                    <br>
                    <span style="font-size: 12px;color: #aaa;">

                        {{post.message_summary}}
                    </span>
                    <span class="glyphicon glyphicon-remove del_feedback" ng-click="deleteFeed(post.id)" style="position:absolute;top: 0%;right: 0%;color: #E13300;font-size: 10px;cursor: pointer;"></span>
                </li>
            </ul>
        </div>

CI控制器

   public function get_feedback()
    {
        $feedback = $this->CmsDbmodel->get_feedback();
        $feedback_json = json_encode($feedback);
        echo $feedback_json;
    }


    public function del_feedback()
    {
        $pid = $_GET['id'];
        $feedback = $this->CmsDbmodel->del_feedback($pid);
    }

CI模型

function  get_feedback()
{
    $query = $this->db->get('feedback');
    return $query->result();
}

function  del_feedback($pid)
{
    $this->db->delete('feedback', array('id' => $pid)); 
}