3 回答

TA貢獻1951條經驗 獲得超3個贊
如果要傳遞非URL狀態,則url在設置時一定不要使用state。我在PR上找到了答案,并四處游逛以更好地理解。
$stateProvider.state('toState', {
templateUrl:'wokka.html',
controller:'stateController',
params: {
'referer': 'some default',
'param2': 'some default',
'etc': 'some default'
}
});
然后,您可以像這樣導航到它:
$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>
該用例已在文檔中完全揭露,但是我認為這是在不使用URL的情況下轉換狀態的有效方法。

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