3 回答

TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊
如果要傳遞非URL狀態(tài),則url在設(shè)置時一定不要使用state。我在PR上找到了答案,并四處游逛以更好地理解。
$stateProvider.state('toState', {
templateUrl:'wokka.html',
controller:'stateController',
params: {
'referer': 'some default',
'param2': 'some default',
'etc': 'some default'
}
});
然后,您可以像這樣導(dǎo)航到它:
$state.go('toState', { 'referer':'jimbob', 'param2':37, 'etc':'bluebell' });
要么:
var result = { referer:'jimbob', param2:37, etc:'bluebell' };
$state.go('toState', result);
因此,在HTML中:
<a ui-sref="toState(thingy)" class="list-group-item" ng-repeat="thingy in thingies">{{ thingy.referer }}</a>
該用例已在文檔中完全揭露,但是我認(rèn)為這是在不使用URL的情況下轉(zhuǎn)換狀態(tài)的有效方法。

TA貢獻(xiàn)1829條經(jīng)驗 獲得超4個贊
內(nèi)森·馬修斯(Nathan Matthews)的解決方案對我不起作用,但這是完全正確的,但沒有解決之道:
關(guān)鍵點是:$ state.go的已定義參數(shù)和toParamas的類型在狀態(tài)轉(zhuǎn)換的兩側(cè)應(yīng)為相同的數(shù)組或?qū)ο蟆?/p>
例如,當(dāng)您按如下所示在狀態(tài)中定義參數(shù)時,則表示參數(shù)是數(shù)組,因為使用了[[]]:
$stateProvider
.state('home', {
templateUrl: 'home',
controller: 'homeController'
})
.state('view', {
templateUrl: 'overview',
params: ['index', 'anotherKey'],
controller: 'overviewController'
})
因此,您也應(yīng)該像這樣將toParams作為數(shù)組傳遞:
params = { 'index': 123, 'anotherKey': 'This is a test' }
paramsArr = (val for key, val of params)
$state.go('view', paramsArr)
您可以通過$ stateParams作為數(shù)組訪問它們,如下所示:
app.controller('overviewController', function($scope, $stateParams) {
var index = $stateParams[0];
var anotherKey = $stateParams[1];
});
更好的解決方案是在兩側(cè)都使用對象而不是數(shù)組:
$stateProvider
.state('home', {
templateUrl: 'home',
controller: 'homeController'
})
.state('view', {
templateUrl: 'overview',
params: {'index': null, 'anotherKey': null},
controller: 'overviewController'
})
我在參數(shù)定義中將{]替換為{}。為了將toParams傳遞給$ state.go,您還應(yīng)該使用object而不是array:
$state.go('view', { 'index': 123, 'anotherKey': 'This is a test' })
那么您可以通過$ stateParams輕松訪問它們:
app.controller('overviewController', function($scope, $stateParams) {
var index = $stateParams.index;
var anotherKey = $stateParams.anotherKey;
});
- 3 回答
- 0 關(guān)注
- 950 瀏覽
添加回答
舉報