【问题标题】:stateparms are attached with current statestateparms 附有当前状态
【发布时间】:2016-11-30 19:28:56
【问题描述】:

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

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 });

【问题讨论】:

    标签: angularjs ionic-framework


    【解决方案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

    【讨论】:

    • @Sarvan 我收到此错误 ""TypeError: Object # has no method 'StateParams'"
    猜你喜欢
    • 2020-08-14
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2022-01-18
    • 2012-10-21
    相关资源
    最近更新 更多