2016-02-10 80 views
0

我创建了一个包含多个表格的数据库,如问题,答案等。我创建了一个视图页并显示数据库中的数据。我也为这个视图创建了一个控制器。我在视图中的每条记录末尾都给出了一个删除按钮。如何删除这个删除按钮的纪录点击应用程序中的从angularjs中删除表中的一行

我的观点

<div class="container" ng-controller="questionEditCtrl"> 

    <form class="form-horizontal" role="form" name='quizEdit' ng-submit="submit(data)"> 
    <div class="table-responsive">   
     <table class="table"> 
      <thead> 
       <tr> 
        <th>#</th> 
        <th>Questions</th> 
        <th>Answer 1</th> 
        <th>Answer 2</th> 
        <th>Answer 3</th> 
        <th>Answer 4</th> 
        <th>Answer 5</th> 
        <th>Correct Answer</th> 
        <th>Action</th> 
       </tr> 
      </thead> 
      <tbody> 
       <?php 
       foreach ($quizes as $q) { 
        ?> 
        <tr> 
         <td ng-model="data.quesID"><?php echo $q['question_id']; ?></td> 
         <td ng-model="data.ques"><?php echo $q['question']; ?></td> 
         <td ng-model="data.ans0"><?php 
          if (isset($q['answers'][0])) { 
           echo $q['answers'][0]; 
          } 
          ?></td> 
         <td ng-model="data.ans1"><?php 
          if (isset($q['answers'][1])) { 
           echo $q['answers'][1]; 
          } 
          ?></td> 
         <td ng-model="data.ans2"><?php 
          if (isset($q['answers'][2])) { 
           echo $q['answers'][2]; 
          } 
          ?></td> 
         <td ng-model="data.ans3"><?php 
          if (isset($q['answers'][3])) { 
           echo $q['answers'][3]; 
          } 
          ?></td> 
         <td ng-model="data.ans4"><?php 
        if (isset($q['answers'][4])) { 
         echo $q['answers'][4]; 
        } 
        ?></td> 
         <td ng-model="data.corr_ans"><?php 
        if (isset($q['correctAnswer'])) { 
         echo $q['correctAnswer']; 
        } 
        ?></td> 
         <td><a href="">Edit</a>/<a href="" ng-click="delete()">Delete</a></td> 

        </tr> 
    <?php 
} 
?> 
      </tbody> 
     </table> 
    </div> 
    </form> 
</div> 

控制器

; 
(function() { 
    'use strict'; 

    angular.module('app', []).controller('questionEditCtrl', ['$scope', '$timeout', '$http', function ($scope, $timeout, $http) { 


      $scope.delete = function() { 
       if (confirm("Are you sure you want to delete this record?")) { 


        // todo code for deletion 

       } 
      }; 

    }]); 

})(); 
+1

http://stackoverflow.com/questions/27002494/delete-data-from-mysql-and-angular – dendimiiii

回答

0

你为什么要使用PHP?这对我来说看起来像是一件可怕的事情。角可以处理你与自己的轻松PHP确实有部分...

(见这里:) Should I mix AngularJS with a PHP framework?

关于你的问题: 你要实现你的服务器和呼叫一个DELETE请求它与您的控制器中的角度使用$ http指令。 之后,您可以使用您可能实现的相应GET请求再次从服务器获取数据,如果您正确执行了所有操作,角将更新视图本身。

但如上所述,这里主要的问题是首先混合角度和php。

+0

你能告诉我怎么写完全使用angularjs对此有何看法?并没有使用PHP ..我不能让ng-repeat工作 – CraZyDroiD

+0

抱歉,但我不认为这样做的地方是这样的。您可能应该先深入了解一些基本的angularjs教程。 的想法是:1)通过$ http从服务器获取数据2)将它放到控制器上的某个变量3)通过ng-repeat在视图中将其可视化。那就是说你应该做好充分的准备,通过我认为的一些教程来工作。 –

0

可能会对你有帮助。 在delete方法中传递该行的id,并使角度ajax post调用到服务器以删除该行。

(function() { 
 
     'use strict'; 
 

 
     angular.module('app', []).controller('questionEditCtrl', ['$scope', '$timeout', '$http', 
 
     function($scope, $timeout, $http) { 
 

 

 
      $scope.delete = function(data) { 
 
      if (confirm("Are you sure you want to delete this record?")) { 
 

 

 
       // todo code for deletion 
 
       // in data variable you will get id of the row and using that id 
 
       // you can make a server call to delete the data using post method you need to modify your delete method i.e make it which accept data as a aurgument or you can passs id of the row is to deleted from talbe 
 

 
      } 
 
      }; 
 

 
     } 
 
     ]); 
 

 
    })();