【发布时间】:2014-02-16 21:33:44
【问题描述】:
我想使用 Angularjs 的 $locationProvider.html5Mode(true) 从 URL 中删除 # 哈希。
示例:地址栏显示http://localhost/shop,而不是http://localhost/#/shop。
在我刷新页面之前一切正常。如果我刷新,将访问以下 Laravel 路由(在 routes.php 中定义)
Route::resource('shop', 'ShoppingController')
不是 AngularJS 路由(在 app.js 中定义)
$routeProvider.when('/shop', {
templateUrl: 'templates/shop.html',
controller: 'ShoppingController'
});
我的代码:
routes.php(Laravel 路由)
Route::get('/', function() {
return View::make('index');
});
Route::resource('shop', 'ShoppingController');
app.js(AngularJS 路由)
var app = angular.module('shoppingApp',['ngRoute','SharedServices']);
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/shop', {
templateUrl: 'templates/shop.html',
controller: 'ShoppingController'
});
$routeProvider.otherwise({ redirectTo: '/' });
$locationProvider.html5Mode(true);
});
我的目录结构:
Project
/app
/...
/views
-index.php (single page application file)
-routes.php (Laravel routes)
/public
/...
/js
-angular.js
-app.js
-index.php (Laravel index file)
尝试过的解决方案:
重写 htaccess 文件,以便将所有请求重定向到 index.php(单页应用程序文件,AngularJS 将从该文件接管路由)。问题:这样,AngularJS $http 服务无法访问 Laravel 路由(Route::resource('shop', 'ShoppingController'); - 与数据库交互所必需的):
app.js
app.controller("ShoppingController", function($scope, $http) {
$http.get('/shop', { cache: true}).
success(function(data, status) {
$scope.items = data
}).
error(function(data, status) {
console.log('Status: ' + status);
});
});
问题: 如何解决路由问题,以便在刷新 localhost/shop 时访问 AngularJS 路由,而不是 Laravel 路由?
【问题讨论】:
标签: html .htaccess hashtag angularjs-routing