stateparms附加当前状态

时间:2016-11-30 11:15:48

标签: angularjs ionic-framework

当我将stateParams传递给另一个状态时,它们会与状态连接并在我的IONIC App中获得以下结果。

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 个答案:

答案 0 :(得分: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对象。

Reference(source) of the above functions