亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用$ state.go toParams和$ stateParams發送和檢索參數?

如何使用$ state.go toParams和$ stateParams發送和檢索參數?

白衣染霜花 2019-11-25 15:18:45
我正在將AngularJS v1.2.0-rc.2與ui-router v0.2.0一起使用。我想將引薦來源網址狀態傳遞給另一個狀態,因此我使用的toParams方式$state.go如下:$state.go('toState', {referer: $state.current.name});根據文檔,這應該填充控制器$stateParams上的toState,但是它是undefined。我想念什么?我創建了一個小例子來演示:http://plnkr.co/edit/ywEcG1
查看完整描述

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的情況下轉換狀態的有效方法。


查看完整回答
反對 回復 2019-11-25
?
浮云間

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;

});


查看完整回答
反對 回復 2019-11-25
  • 3 回答
  • 0 關注
  • 954 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號