【发布时间】:2016-02-21 07:56:59
【问题描述】:
我是 Laravel 5 和 Angular 的新手。 我将 Laravel 路由用于遍历和后端操作,使用 Angular 仅用于 UI 操作,例如获取数据和绑定 UI 网格等。
我在下面的 routes.php 文件中定义了以下路由
routes.php
Route::pattern('clientid', '[0-9]+');
//used for AJAX call from angularjs and populating ui-grid
Route::get('getclients/{clientid?}', 'ClientController@getClients');
//used for displaying Laravel view with ui-grid
Route::get('client/{clientid?}', 'ClientController@showClients');
请查找角度文件:
app.js
var appClients = angular.module('getclients', ['clientsService', 'ui.grid', 'ui.grid.exporter', 'ui.grid.selection']);
clientController.js
appClients.controller('ClientsController', ['$scope', '$http', 'Client', '$interval', '$q', function ($scope, $http, Client, $interval, $q) {
/* Defining UI grid options*/
.
.
/* Calling service to fill the grid*/
Client.get(clientid)
.success(function (data, status, headers, config) {
if (data.length > 0) {
$scope.gridOptions.data = data;
}
});
}
clientsService.js
angular.module('clientsService', [])
.service('Client', function ($http) {
return {
// Get all the photos
get: function (clientid) {
if (clientid !== '') {
return $http.get('/myproject/public/getclients/' + clientid);
}
else {
return $http.get('/myproject/public/getclients/');
}
}
}
});
/*
**Note:**
Have already defined route in routes.php for using the same above:
Route::get('getclients/{clientid?}', 'ClientController@getClients');
*/
示例:
Step 1:
Say I am hitting URL: http://<domain>/public/myproject/client/2
The following route would catch it and redirect to view where the ui-grid is present
Route::get('client/{clientid?}', 'ClientController@showClients');
Step 2:
Now, somehow need to figure out how to pass that **2** to angular so that I could pass that parameter while making ajax call and get grid data
我很困惑如何在 angular 中使用 Laravel 的 url 参数?
我认为我在这里遗漏了一些概念或做错了什么。
谁能帮帮我?
【问题讨论】:
-
为什么不获取 url 参数并对你的控制器进行 http 调用并完成它?
-
无法找到您。我们需要在 http 调用之前以某种方式获取 URL 参数。
-
无法获取
routeParams? -
没有使用
ng-Route,因为没有角度页面。那么如何在不使用ng-Route的情况下获得routeParams? -
那么你可以在那个特定区域使用jquery吗?
标签: angularjs routes laravel-5