使用angular ui-router我正在尝试根据condition.i.e进行嵌套路由。想要在加载状态之前检查条件。
.state('main.home', {
url: "/:cnt",
abstract: true,
templateUrl: function ($stateParams) {
return template1;
},
controller: "myController",
resolve: {
//Some model
}],
lazy: ['$ocLazyLoad', '$stateParams', function ($ocLazyLoad, $stateParams) {
//lazily loaded controllers
}]
},
onEnter: updateAppValues,
}).state('main.home.default', {
url: '',
templateUrl: function ($stateParams) {
return template2;
},
resolve: {
lazy: ['$ocLazyLoad', function ($ocLazyLoad) {
//lazily loaded controllers
}]
},
controller: 'myDefaultController',
})
基本上必须有条件地加载嵌套路由器main.home.default
if(something){
//loaded state main.home.default
}
我怎样才能做到这一点?
答案 0 :(得分:1)
您可以通过以下方式捕捉路线更改事件。
public function redirect()
{
return \Socialite::driver('facebook')->redirect();
//return $providerUser;
}
public function callback(SocialAccountService $service)
{
$user = $service->createOrGetUser(Socialite::driver('facebook')->user());
var_dump($user);
dd($user);
auth()->login($user);
return redirect()->to('/home');
}
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if(next.$$route == 'routeYouWantToAvoid') { // Or whatever you want
$state.transitTo('main.home.default');
}
});
是您要前往的路线。
next
您来自的路线。
如果您需要更多信息,请参阅documentation。
答案 1 :(得分:0)
app.run(function($rootScope, $state) {
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
if (toState.name == 'login'){ //if the name of that state is login
//go to login page
}
});
});
这会给你带来什么想法吗?或
app.run(function($rootScope, $state){
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
if(*your condition*) {
$state.transitTo('stateYouWantToGo')
event.preventDefault()
}
});
});
答案 2 :(得分:0)
您可以在main.home.default的解析中添加if条件。例如:
resolve:{
"check":function($location){
if('Your Condition'){
//Do something
}else{
$location.path('/'); //redirect user to home.
alert("You dont belong here");
}
}
}