4

I wrote below code in my app.js file (this is a rest api angularjs project)

.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");
             }

         );
     };
}])

first alert box and second alert box is apearing. but

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

is not working. Questions.delete({id:id}) is "true". but why don't it go inside?.

Here are my other codes for delete function.

from template list.html

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

rest model class

private function delete($id) {     

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

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

    return NULL;    

}

rest controller class

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);

    }   

    } 

}

can some one tell me what is missing here?. Thanks,

2
  • Does the question is actually being deleted from the DB? Don't you need to check if(data.response.deleted){ (If not - write console.log(data) in the promise success callback to see what is actually returned from the server) Commented Mar 26, 2016 at 9:02
  • data.response.deleted didn't work for me Commented Mar 26, 2016 at 15:22

1 Answer 1

1

There has been a little mistake. When I changed private delete to public delete it worked finally.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.