2016-03-26 56 views
3

我在app.js文件中寫道:下面的代碼(這是一個REST API angularjs項目)刪除功能不能正常工作angularjs

.controller('HomeCtrl'['$scope','Questions','$route',function($scope,Questions,$route) { 

    var id = $route.id; 

    Questions.get(function(data) {    
     $scope.questions = data.response; 
    }); 

    $scope.remove = function(id) { 

     window.alert("works"); 
     //var id = $scope.question[id]; 
     // document.write(id); 

     Questions.delete({id:id}).$promise.then(function(data) { 
      if(data.response){     
       $route.reload();     
      }     
     } , function(){ 
       alert("2nd"); 
      } 

     ); 
    }; 
}]) 

第一警告框和第二警報框apearing。但是

Questions.delete({id:id}).$promise.then(function(data) { 
    if (data.response) {     
     $route.reload();     
} 

不工作。 Questions.delete({id:id})是「true」。但爲什麼不進去呢?

這裏是我的其他代碼刪除功能。

從模板list.html

<a class="trash" ng-click="remove(question.id)"><span class="glyphicon glyphicon-trash"></span></a> 

其餘模型

private function delete($id) {  

    $this->db->where('id',$id)->delete('questions'); 

    if ($this->db->affected_rows() === 1) { 
     return TRUE; 
    } 

    return NULL;  

} 

其他類控制器類

public function index_delete($id) { 

    if (!$id) { 
     $this->response(NULL,400); 
    } 

    $delete = $this->questions_model->delete($id); 

    if(!is_null($delete)) { 

      $this->response(array("response" =>"deleted"),200); 

    } else { 

     $this->response(array("error" => "cud not save"),404); 

    } 

    } 

} 

可以有一個人告訴我這到底是怎麼丟失的? 謝謝,

+0

問題是否真的從數據庫中刪除?你不需要檢查'if(data.response.deleted){'(如果不是 - 在promise promise回調中寫入'console.log(data)',看看服務器實際返回了什麼) –

+0

data.response .deleted沒有爲我工作 –

回答

0

有一個小小的錯誤。當我更改私人刪除公共刪除它終於工作。