我在 ui-router 中遇到嵌套 View 的问题。
我的布局是:
site.search ----------------- site.search.runs ----------------- site.search.runs.scheme ----------------- site.search.runs.scheme.payment -----------------
I don't want to change url that's why I use $state.go('', {}, {location:false});
I also use $stage.go('^'...)
for back to parent.
But when I click on Back button on site.search.runs.scheme.payment or site.search.runs.scheme - site.runs controller also reloads (I don't use {reload:true}).
How to prevent reloading top level parent controller ?
I have found a problem like mine https://github.com/angular-ui/ui-router/issues/2096
Can anybody help me ?
My configuration is
$stateProvider
.state('site', {
abstract: true,
url: '',
template: '<ui-view/>'
})
.state('site.search', {
url: '',
parent: 'site',
controller: 'SearchController',
controllerAs: 'search',
templateUrl: 'js/search/search.tpl.html'
})
.state('site.search.runs', {
url: '',
parent: 'site.search',
params: {back: false, timestamp: 0, search: {}},
controller: 'RunsController as runsCtrl',
templateUrl: 'js/runs/runs.tpl.html'
})
.state('site.search.runs.scheme', {
url: '',
parent: 'site.search.runs',
params: {back: false, run: ''},
controller: 'SchemeController as schemeCtrl',
templateUrl: 'js/scheme/scheme.tpl.html'
})
.state('site.search.runs.scheme.payment', {
url: '',
parent: 'site.search.runs.scheme',
params: {back: false},
controller: 'PaymentController as payCtrl',
templateUrl: 'js/payment/liqpay/payment.tpl.html'
})
});
Back 方法调用这个 $state.go('^', {back: true}, {location: false, inherit: false});
最佳答案
你可以做的一个肮脏的把戏是将 Controller 代码包装在一个 if 语句中并执行类似这样的操作(这绝不是最好的方法,但它肯定会起作用):
// make sure you inject $state
// and put the if statement inside the main function of your controller
function RunsController($state) {
// var will return something like: site.search.runs.scheme
var currentState = $state.current.name;
if (currentState === 'site.search.runs') {
// in here put the code that would normally be in this controller for site.search.runs
}
}
关于javascript - 防止在 ui-router 中重新加载父 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32318966/