2016-11-30 44 views
1

當我將stateParams傳遞給另一個狀態時,它們與狀態連接並在IONIC應用程序中獲得以下結果。stateparms附帶當前狀態

var $currState = $ionicHistory.currentView().stateId; 
$scope.consoleLog('$currState: ' + $currState); //$currState: app.stateA_purchaseData=[object Object]_supplierData=[object Object]" 

$scope.consoleLog('$stateParams: ' + JSON.stringify($stateParams)); //$stateParams: {} 

,這裏是

state('app.StateA', { 
     url: '/test-url', 
     templateUrl: 'templates/test.html', 
     controller: 'AppCtrl', 
     cache: false, 
     params: { 
      purchaseData: null, 
      supplierData: null, 
     } 
    }) 


$state.go('app.StateA', {purchaseData: $scope.purchaseData, supplierData: $scope.supplierData }); 

回答

1

這是因爲發生的配置中,Ionic history的文檔中有一個方法getCurrentStateId()該地連接了statenameparams

Check from line no 142 in ionic_history.js in github documentation

function getCurrentStateId() { 
    var id; 
    if ($state && $state.current && $state.current.name) { 
     id = $state.current.name; 
     if ($state.params) { 
     for (var key in $state.params) { 
      if ($state.params.hasOwnProperty(key) && $state.params[key]) { 
      id += "_" + key + "=" + $state.params[key]; 
      } 
     } 
     } 
     return id; 
    } 
    // if something goes wrong make sure its got a unique stateId 
    return ionic.Utils.nextUid(); 
    } 

要得到的參數,而是 試,

StateParams()這就要求getCurrentStateParams()

function getCurrentStateParams() { 
    var rtn; 
    if ($state && $state.params) { 
     for (var key in $state.params) { 
     if ($state.params.hasOwnProperty(key)) { 
      rtn = rtn || {}; 
      rtn[key] = $state.params[key]; 
     } 
     } 
    } 
    return rtn; 
    } 

這實際上返回params對象y的OU。

Reference(source) of the above functions

+0

@Sarvan我得到這個錯誤 「」 類型錯誤:對象#有沒有方法 'StateParams'」 –