2016-11-11 174 views
0

首先,我很新的Laravel + Angular這基本上是我爲什麼在這裏。如何從Angular控制器調用Laravel控制器的功能?

我有這個功能從我的Angular控制器中刪除,使用$http.delete()來調用Laravel控制器。

$scope.deleteJob = function(index) { 
    $scope.loading = true; 

    var job = $scope.jobs[index]; 

    $http.delete('/api/job/' + job.jobId) 
     .success(function() { 
      $scope.job.splice(index, 1); 
      $scope.loading = false; 
     });; 
}; 

有了這條路,我應該得到的Laravel控制器:

Route::resource('api/job','ApiJobController'); 

我所定義的函數從我的口才DB刪除

public function destroy($id) 
{ 
    App\Job::destroy($id); 
} 

我不知道是什麼我想念,但每次我調用deleteJob(index)我得到一個服務器錯誤。

DELETE http://localhost:8000/api/job/20 500 (Internal Server Error) 

我試着用/api/job/destroy/,仍然得到500

+0

什麼是響應錯誤? – aseferov

+0

@aseferov無法加載資源:服務器迴應狀態爲500 –

回答

0

與這條路線

Route::get('/api/job','[email protected]'); 

嘗試或者您可以使用

$http({ 
    method: 'DELETE', 
    url: '/api/job/' + job.jobId 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 
+0

快速問題,如果我有另一個函數從ApiJobController調用怎麼辦?這條路線是否會限制我的功能銷燬?謝謝 –

+0

您的方法類型和url將決定與函數需要調用,請檢查laravel REST API,因爲您正在使用資源路徑 – C2486

0

試試這個。

App\Job::find($id)->delete();

+0

仍然返回500.我認爲問題是,我無法與laravel控制器jobController @ destroy溝通,並且請簡要說明您希望我嘗試的代碼,謝謝! @ sahil09 –

+0

@HardSpocker您可以在Job model – sahil09

+0

中顯示您的銷燬($ id)方法中的代碼,您是指該模型?即時通訊使用雄辯的模式它不包含任何東西 –

相關問題