2015-10-06 72 views
0

我使用laravel 5.1,请求如何接受id通过api调用传递角度?这是我的代码。角度传递参数Laravel编辑

angular.module('MyApp') 
    .factory('Account', function($http){ 
     return { 
getProductToEdit: function(pid){ 
       //console.log(pid) ----> return 16 
       return $http.get('/api/productedit',pid); 
      } 
    } 
    }); 

我的支持功能:

public function updateProductAPI(Request $request) 
    { 
     $prodId = $request->all(); ---> return [] 
     // $request->input('pid'); ---> Object {} 

     return response()->json($prodId); 
    } 

谢谢!

回答

4

传递给$http.get函数的第二个对象是一个配置对象。您需要在此对象上设置params属性以与请求一起发送数据。

$http.get('url', { params : { pid : pid } }); 

You can read more about the config object here.

现在你的控制器内

public function updateProductAPI(Request $request) 
{ 
    // Get the id 
    $id = $request->input('pid'); 
}); 

把API一起时,另一种方法是利用路由参数来处理的唯一标识符,如IDS或蛞蝓。

// The get call 
$http.get('url/'+pid).then(function(data) { ... }); 

// routes.php 
Route::get('url/{pid}', '[email protected]'); 

// Controller method 
public function updateProductAPI($id, Request $request) 
{ 
    // $id available here 
}