我有一个项目,laravel 5作为后端,angularjs 1.3作为前端。
Laravel5设置:
//routes.php
//Routes for api to get json
Route::group('prefix'=>'api', function(){
Route::controllers([
'subjects' => 'SubjectsController',
'pages' => 'PagesController',
'articles' =>'ArticlesController'
]);
// route for angular
Route::get('/dash', 'DashController@index);
// handler.php
// for catch all routing
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException)
{
return response()->view('ajs-dash.index')->header('Content-Type', 'text/html');
}
return parent::render($request, $e);
}
在Angular方面
//dash.js
angular.module('dash', [
'dash.content', 'dash.content.controller', 'dash.app.controller',
'ui.router', 'ui.bootstrap', 'ui.tinymce'])
.constant('USER_ROLES', {
default : '*',
super : 'super',
admin : 'admin',
manager : 'manager',
guest : 'guest'
})
.run(['$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams){
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
$rootScope.$on('$stateChangeStart', function(event, next){
var authorizedRoles=[],
authorized = false,
loggedIn=false;
if(next.authorizedRoles){
authorizedRoles = next.authorizedRoles;
authorized = (authorizedRoles.indexOf($rootScope.userRole) !== -1);
loggedIn = $rootScope.loggedIn;
}
//console.log('next = '); console.log(next);
//console.log('authorizedRoles = '); console.log(authorizedRoles);
console.log('$rootScope.userRole= '); console.log($rootScope.userRole);
console.log('$rootScope.loggedIn= '); console.log($rootScope.loggedIn);
if(!loggedIn && !authorized){
event.preventDefault();
}
})
}])
.config(['$stateProvider', '$urlRouterProvider', 'USER_ROLES',
function($stateProvider, $urlRouterProvider, USER_ROLES){
//$urlRouterProvider.otherwise('/');
$stateProvider
.state('dash', {
abstract:false,
url:'/',
templateUrl:'/admin/views/dash.html',
authorizedRoles:[USER_ROLES.admin, USER_ROLES.super]
});
}]);
其余的状态在content.js中定义,即dash.content模块
在dash中的dash.php是我引导角度应用程序的地方&#39; dash&#39;通过在<html>
标记上添加data-ng-app =&#34; dash&#34;
当我浏览\dash
时,视图正在加载,从那里我甚至能够正确地导航子状态。
但问题是,在\dash
上时,短划线的本地状态未设置。如果我在dash.php中使用ui-sref =&#34; dash&#34;然后单击它然后状态正在设置。但它并没有通过导航到网址来设置
另一个问题是,无论何时在任何状态下,无论是破折号还是任何子状态,如果我刷页面都会丢失所有状态。
我在用
$state = {{$state.current.name}} $stateParams = {{$stateParams}} $state full url = {{$state.$current.url.source}}
我错过了什么或做错了什么?