将动态URL值传递给角度js控制器

问题描述 投票:0回答:1

我想传递url参数$ id是Angularjs控制器的动态值

我的Angularjs:

$http.get('/todos/show/'+$id).success(function(data) {
  $scope.customers = data;
});

我的路线在laravel

Route::get('todos/show/{id?}', function($id = null)
{
  $users = DB::table('customers')->where('id', $id)->get();
  return json_encode($users);
});

有人可以帮助我这些。

php angularjs laravel angularjs-directive angularjs-scope
1个回答
0
投票

===也许我弄错了。最后看到正确的答案。===

首先,您需要设置您的routerProvider:https://www.w3schools.com/angular/angular_routing.asp

之后,您可以设置这样的路线:

app.config(function($routeProvider) {
    $routeProvider.when("/todos/show/:id", {
        //your content here like example of w3schools
    })
});

要么

app.config(function($routeProvider) {
    $routeProvider.when("/todos/show/:id?", {
        //your content here like example of w3schools
    })
});

如果id是可选字段。

现在,您需要将$ routeParams传递给您的控制器,并且您可以获取路径数据:

var myApp = angular.module('myApp',[]);

myApp.controller('Controller', ['$scope', '$routeParams', function($scope, $routeParams) {
  console.log($routeParams.id)
}]);

===正确答案===

用帖子!角度:

$http.post('/todos/show', {id: $id}).success(function(data) {
  $scope.customers = data;
});

Laravel:

Route::post('todos/show', function(Request $request)
{
  $users = DB::table('customers')->where('id', $request->input('id'/*, 'optional default value'*/))->get();
  return json_encode($users);
});
© www.soinside.com 2019 - 2024. All rights reserved.